using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; // 추가된 참조 using System.IO; using System.Net; using System.Diagnostics; namespace ISBN_Client { public partial class Client : Form { // FTP ID / PW private string Login_id = "ftpgloria"; private string Login_pw = "admin@!@#$"; // 서버 / 클라이언트 버전 private string Sr_Vers = ""; private string Cl_Vers = ""; // 서버 아이피 private string Server_Ip = ""; // 종료시 실행시킬 파일명 private string Start_Prg = ""; // 파일 개수 private int Files_Count = 0; // 업데이트 진행 파일 수 private int down_Count = 0; // 업데이트 여부 bool tf = false; private string sLine = ""; private string[] str = new string[2]; int i = -1; DataSet ds = new DataSet("files"); public Client() { InitializeComponent(); } private void Client_Load(object sender, EventArgs e) { try { // 파일 목록 생성을 위한 데이터 셋 ds.Tables.Add("파일"); ds.Tables["파일"].Columns.Add("file_name"); ds.Tables["파일"].Columns.Add("chk"); File_info(); dataGridView1.DataSource = ds.Tables["파일"]; // 서버의 update_isbn.inf 파일에서 버전 추출 FtpWebRequest fwr = (FtpWebRequest)WebRequest.Create("ftp://" + Login_id + "@" + Server_Ip + "/ISBN/Update_isbn.inf"); fwr.Credentials = new NetworkCredential(Login_id, Login_pw); fwr.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse fr = (FtpWebResponse)fwr.GetResponse(); StreamReader sr = new StreamReader(fr.GetResponseStream()); while (!sr.EndOfStream) { sLine = sr.ReadLine(); i = sLine.IndexOf("count=", 0); // 서버 버전 추출 if(sLine.IndexOf("count=", 0) != -1) { Sr_Vers = sLine.Replace("count=", ""); lbl_SerVer.Text = Sr_Vers; break; } } sr.Close(); // 버전이 같은 경우 버튼 변경 if (Convert.ToDecimal(Sr_Vers) == Convert.ToDecimal(Cl_Vers)) { btn_ok.Text = "프로그램 실행"; lbl_status.Text = "최신 버전입니다!"; lbl_status.ForeColor = Color.Blue; lbl_filename.Text = ""; lbl_cnt.Text = ""; for(int a = 0; a < dataGridView1.Rows.Count; a++) { dataGridView1.Rows[a].Cells["update_status"].Value = "T"; } btn_ok_Click(null, null); } else if (Convert.ToDecimal(Sr_Vers) > Convert.ToDecimal(Cl_Vers)) { btn_ok.Text = "업데이트"; tf = true; lbl_cnt.Text = "(1/" + dataGridView1.Rows.Count.ToString() + ")"; lbl_status.Text = "업데이트가 존재합니다!"; lbl_status.ForeColor = Color.DeepPink; } } catch(System.Exception ex) { } } private void btn_ok_Click(object sender, EventArgs e) { if (tf) { // 업데이트가 존재할 때 download(0); } else { // 업데이트가 없을 때 string start_program = Application.StartupPath + "\\" + Start_Prg; Process prc = new Process(); prc.StartInfo = new System.Diagnostics.ProcessStartInfo(start_program); prc.Start(); this.Close(); } } private void download(int cnt) { if (cnt < Convert.ToInt32(lbl_Files.Text)) { WebClient clnt = new WebClient(); clnt.Credentials = new NetworkCredential(Login_id, Login_pw); lbl_status.Text = "업데이트 진행중!"; progressBar1.Value = (progressBar1.Maximum / Convert.ToInt32(lbl_Files.Text)) * (down_Count + 1); lbl_filename.Text = dataGridView1.Rows[cnt].Cells["file_name"].Value.ToString(); dataGridView1.Rows[cnt].Cells["update_status"].Value = "T"; lbl_cnt.Text = "(" + (cnt + 1).ToString() + "/" + (dataGridView1.Rows.Count).ToString() + ")"; File.Delete(Application.StartupPath + lbl_filename.Text); clnt.DownloadFileAsync(new Uri("ftp://" + Login_id + "@" + Server_Ip + "/ISBN/" + lbl_filename.Text), Application.StartupPath + "\\" + lbl_filename.Text); clnt.DownloadFileCompleted += new AsyncCompletedEventHandler(clnt_DownloadFileCompleted); } else { File_info(); progressBar1.Value = progressBar1.Maximum; btn_ok.Text = "프로그램 실행"; lbl_status.Text = "최신 파일입니다!"; lbl_status.ForeColor = Color.Blue; lbl_filename.Text = ""; lbl_cnt.Text = ""; tf = false; } } void clnt_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { down_Count += 1; download(down_Count); } private void File_info() { // 클라이언트 파일 정보 // update.inf 파일에서 파일개수, 파일명, 버전 추출 StreamReader sr = new StreamReader(Application.StartupPath + "\\Update_isbn.inf"); int i = -1; while(sr.EndOfStream != true) { sLine = sr.ReadLine(); i = sLine.IndexOf("count=", 0); // 버전 추출 if (sLine.IndexOf("count=", 0) != -1) { Cl_Vers = sLine.Replace("count=", ""); lbl_ClientVer.Text = Cl_Vers; } // 설치 경로 추출 else if (sLine.IndexOf("server_url=", 0) != -1) { Server_Ip = sLine.Replace("server_url=", ""); rtb_Ip.Text = Server_Ip; //rtb_Ip.Text = Application.StartupPath; } // 종료시 실행 파일 else if (sLine.IndexOf("exe=", 0) != -1) { Start_Prg = sLine.Replace("exe=", ""); } // 파일 개수 추출 else if (sLine.IndexOf("Files=", 0) != -1) { Files_Count = Convert.ToInt32(sLine.Replace("Files=", "")); lbl_Files.Text = Files_Count.ToString(); } else if(sLine.IndexOf("\\", 0) != -1) { str[0] = sLine.Replace("\\", ""); str[1] = "F"; ds.Tables["파일"].Rows.Add(str); } i = -1; } sr.Close(); } private void btn_Close_Click(object sender, EventArgs e) { this.Close(); } } }