private void Bytesdownloaded(ByteArgs e) { progressBarUpdate.Maximum = e.total; if (progressBarUpdate.Value + e.downloaded <= progressBarUpdate.Maximum) { progressBarUpdate.Value += e.downloaded; var sizeSuffix = AppSettings.SizeSuffix(e.total); if (!string.IsNullOrEmpty(sizeSuffix)) { lblDownloadedSize.Content = sizeSuffix; } } else { } }
public static bool GetUpdate(string _updateUrl, string _file) { try { if (!string.IsNullOrEmpty(_updateUrl)) { if (!string.IsNullOrEmpty(_file)) { byte[] downloadedData = new byte[0]; var webReq = WebRequest.Create(_updateUrl); using (var webResponse = webReq.GetResponse()) { using (var dataStream = webResponse.GetResponseStream()) { //Download chuncks byte[] dataBuffer = new byte[1024]; //Get size int dataLength = (int)webResponse.ContentLength; ByteArgs byteArgs = new ByteArgs(); byteArgs.downloaded = 0; byteArgs.total = dataLength; if (bytesDownloaded != null) { bytesDownloaded(byteArgs); } //Download using (MemoryStream memoryStream = new MemoryStream()) { while (true) { int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length); if (bytesFromStream == 0) { byteArgs.downloaded = dataLength; byteArgs.total = dataLength; if (bytesDownloaded != null) { bytesDownloaded(byteArgs); } //Download complete break; } else { //Write the downloaded data memoryStream.Write(dataBuffer, 0, bytesFromStream); byteArgs.downloaded = bytesFromStream; byteArgs.total = dataLength; if (bytesDownloaded != null) { bytesDownloaded(byteArgs); } } } //Convert to byte array downloadedData = memoryStream.ToArray(); //Write bytes to the specified file if (Directory.Exists(_targetUpdateFolder)) { using (FileStream newFile = new FileStream(_targetUpdateFolder + "\\" + _file, FileMode.Create)) { newFile.Write(downloadedData, 0, downloadedData.Length); } } return true; } } } } } } catch (Exception ex) { //MessageBox.Show(ex.Message + " GetUpdate()"); GeneralSettings.LogException(ex); } return false; }