/* * * Metoda pobierająca aktualizacje z serwera ftp. */ private void BackgroungWorker_DoWork(object sender, DoWorkEventArgs e) { try { String strAppDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().GetName().CodeBase); this._localPath = strAppDir; FileInfo fileInfo = new FileInfo(name2); FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serwer1585870.home.pl/SB/" + name2); request.Credentials = new NetworkCredential("pz1-isk1-sggw", "ppz1-isk1-sggw"); Boolean UsePassive = true; Boolean UseBinary = true; request.Method = WebRequestMethods.Ftp.GetFileSize; request.Proxy = null; request.KeepAlive = true; request.UsePassive = UsePassive; request.UseBinary = UseBinary; long fileSize; using (WebResponse resp = request.GetResponse()) fileSize = resp.ContentLength; request = (FtpWebRequest)WebRequest.Create("ftp://serwer1585870.home.pl/SB/" + name2); request.Credentials = new NetworkCredential("pz1-isk1-sggw", "ppz1-isk1-sggw"); using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse()) using (Stream responseStream = responseFileDownload.GetResponseStream()) using (FileStream writeStream = new FileStream(name2, FileMode.Create)) { long length = responseFileDownload.ContentLength; int Length = 2048; Byte[] buffer = new Byte[2048]; int bytesRead = responseStream.Read(buffer, 0, Length); int bytes = 0; while (bytesRead > 0) { writeStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, Length); bytes += bytesRead; int totalSize = (int)(fileSize) / 1000; // Kbytes BackgroungWorker.ReportProgress((bytes / 1000) * 100 / totalSize, totalSize); } responseFileDownload.Close(); writeStream.Close(); } } catch (Exception) { MessageBox.Show("Nie udało się ustalić połączenia z hostem."); Process.GetCurrentProcess().Kill(); } finally { completed = true; lbProgress.Text = "100 %"; } }
/* * * Metoda deklarująca warunki, które muszą zostać spełnione aby aktualizacja zostałą rozpoczęta. */ public void doIT() { if (File.Exists(path + "\\SmartSell.exe")) { if (completed == false) { VersionChecking(); } WaitToUnzip.Start(); } else if (!File.Exists(path + "\\SmartSell.exe")) { if (completed == false) { BGworkersWithData.Add(BackgroungWorker); BackgroungWorker.RunWorkerAsync(); CheckAllThreadsHaveFinishedWorking(); } WaitToUnzip.Start(); } }
//Metoda, która może zostać wykorzystana w przyszłości, ponawiająca połączenie. /*public static class Retry * { * public static void Do( * Action action, * TimeSpan retryInterval, * int retryCount = 3) * { * Do<object>(() => * { * action(); * return null; * }, retryInterval, retryCount); * } * * public static T Do<T>( * Func<T> action, * TimeSpan retryInterval, * int retryCount = 3) * { * var exceptions = new List<Exception>(); * * for (int retry = 0; retry < retryCount; retry++) * { * try * { * if (retry > 0) * Thread.Sleep(retryInterval); * return action(); * } * catch (Exception ex) * { * exceptions.Add(ex); * } * } * * throw new AggregateException(exceptions); * } * } * * Metoda sprawdzająca wersje aplikacji. */ private void VersionChecking() { var versionInfo = FileVersionInfo.GetVersionInfo(path + "\\SmartSell.exe"); string ver = versionInfo.ProductVersion; DownloadCompletedtxt.Visible = false; WebClient request = new WebClient(); string url = "ftp://serwer1585870.home.pl/SB/" + "wersja.txt"; request.Credentials = new NetworkCredential("pz1-isk1-sggw", "ppz1-isk1-sggw"); try { byte[] newFileData = request.DownloadData(url); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); if (ver != fileString) { /*DialogResult dialog = MessageBox.Show("Istnije nowsza wersja: " + fileString + * "\n Czy pobrać ją teraz ?", "Aktualizacja", MessageBoxButtons.YesNo, * MessageBoxIcon.Information); * if (dialog == DialogResult.Yes) * {*/ BGworkersWithData.Add(BackgroungWorker); BackgroungWorker.RunWorkerAsync(); CheckAllThreadsHaveFinishedWorking(); } else { CHCKUPD.Text = "Your version is up to date!"; UpToDateRun.Start(); } } catch (WebException) { MessageBox.Show("Sprwadź połączenie sieciowe, nie ma możliwości połączenia się z hostem!"); } }