//Example 1 public static void SimpleZip(string dirToZip, string zipName, MainWnd mainWnd, CustomProgressBar pb_ZIP) { CancellationTokenSource ts_ZIP = new CancellationTokenSource(); CancellationToken ct_ZIP = ts_ZIP.Token; try { Task pbZip_Task = new Task(() => { pb_ZIP.SetProgressBarValueByTicks(mainWnd, 100, "Zipping...", ct_ZIP); }, ct_ZIP); pbZip_Task.Start(); ZipFile.CreateFromDirectory(dirToZip, zipName); ts_ZIP.Cancel(); mainWnd.SetProgressBarValue(100, "Zipping finished", pb_ZIP); } catch (IOException exc) { ts_ZIP.Cancel(); MessageBox.Show(exc.Message); DialogResult dialogResult = MessageBox.Show("Do you whant to delete existing file(zip) ?", "File exists !!", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { mainWnd.SetProgressBarValue(0, "", pb_ZIP); File.Delete(zipName); SimpleZip(dirToZip, zipName, mainWnd, pb_ZIP); } else { MessageBox.Show("Old version of file(zip) will be uploaded !!"); mainWnd.SetProgressBarValue(100, "Zipping finished", pb_ZIP); } } catch (Exception exc) { MessageBox.Show(exc.Message); throw; } }
public static void ZipThenFtp(MainWnd mainWnd, TextBox tb_selectedPublishItemFullPath, CustomProgressBar pb_ZIP, CustomProgressBar pb_FTP, string SiteName) { string directoryForZipFtp = tb_selectedPublishItemFullPath.Text; string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name; currentUser = currentUser.Contains(@"\") == true?currentUser.Replace('\\', '_') : currentUser; string zipFileName_Path = directoryForZipFtp + "_" + SitesLogic.Frontend + "_" + currentUser + ".zip"; Compression.SimpleZip(directoryForZipFtp, zipFileName_Path, mainWnd, pb_ZIP); string fileName = zipFileName_Path.Substring(zipFileName_Path.LastIndexOf('\\') + 1); fileName = SiteName + "_" + fileName; using (WebClient client = new WebClient()) { CancellationTokenSource ts_FTP = new CancellationTokenSource(); CancellationToken ct_FTP = ts_FTP.Token; Task pbFtp_Task = new Task(() => { pb_FTP.SetProgressBarValueByTicks(mainWnd, 100, "Uploading...", ct_FTP); }, ct_FTP); pbFtp_Task.Start(); client.Credentials = new NetworkCredential(UserName, Password); client.UploadFile(FTP_URL + fileName, WebRequestMethods.Ftp.UploadFile, zipFileName_Path); ts_FTP.Cancel(); mainWnd.SetProgressBarValue(100, "Upload Finished", pb_FTP); } MessageBox.Show("upload finished !!"); File.Delete(zipFileName_Path); mainWnd.SetProgressBarValue(0, "", pb_ZIP); mainWnd.SetProgressBarValue(0, "", pb_FTP); }
public void SetProgressBarValueByTicks(MainWnd mainWnd, int value, string text, CancellationToken cancellationToken) { try { for (int i = 0; i < value; i++) { mainWnd.SetProgressBarValue(i, text, this); Thread.Sleep(200); cancellationToken.ThrowIfCancellationRequested(); } } catch (Exception) { } }