private void DownloadFile(string sourceFile, string destinationFile, ProgressBar progressBar) { try { Log.WriteStart("Downloading file"); Log.WriteInfo(string.Format("Downloading file \"{0}\" to \"{1}\"", sourceFile, destinationFile)); lblValue.Text = string.Empty; long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar lblValue.Text = string.Format("{0} KB of {1} KB", downloaded / 1024, fileSize / 1024); progressBar.Value = Convert.ToInt32((downloaded * 100) / fileSize); if (content.Length < ChunkSize) { break; } } lblValue.Text = string.Empty; Log.WriteEnd(string.Format("Downloaded {0} bytes", downloaded)); } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }
private void DownloadFile(string sourceFile, string destinationFile, ProgressBar progressBar) { try { long downloaded = 0; long fileSize = service.GetFileSize(sourceFile); if (fileSize == 0) { throw new FileNotFoundException("Service returned empty file.", sourceFile); } byte[] content; while (downloaded < fileSize) { content = service.GetFileChunk(sourceFile, (int)downloaded, ChunkSize); if (content == null) { throw new FileNotFoundException("Service returned NULL file content.", sourceFile); } FileUtils.AppendFileContent(destinationFile, content); downloaded += content.Length; //update progress bar progressBar.Value = Convert.ToInt32((downloaded * 100) / fileSize); if (content.Length < ChunkSize) { break; } } } catch (Exception ex) { if (Utils.IsThreadAbortException(ex)) { return; } throw; } }