using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using WebCamService; using System.IO; namespace FastGallery { internal partial class FtpProgress : Form { private FtpSite ftpSite; public FtpProgress(FtpSite ftpSite) { InitializeComponent(); this.ftpSite = ftpSite; } private void cancelButton_Click(object sender, EventArgs e) { visualBackgroundWorker1.CancelAsync(); } private void FtpProgress_Load(object sender, EventArgs e) { visualBackgroundWorker1.RunWorkerAsync(); } private void visualBackgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { visualBackgroundWorker1.ReportProgress(0, "Opening connection to " + ftpSite.SiteURL); FtpClient ftpClient = new FtpClient(ftpSite.SiteURL, ftpSite.UserName, ftpSite.Password); ftpClient.FileSendingEvent += new FileSendingEvent(ftpClient_FileSendingEvent); try { ftpClient.Login(); if (visualBackgroundWorker1.CancellationPending) { throw new CancelledException(); } visualBackgroundWorker1.ReportProgress(0, "Creating remote directory"); ftpClient.ChangeDir(ftpSite.InitialDirectory); try { ftpClient.MakeDir(Program.Settings.Gallery.Name); } catch (FtpClient.FtpException) { } if (visualBackgroundWorker1.CancellationPending) { throw new CancelledException(); } // get files for progress numberFiles = Directory.GetFiles(Program.Settings.Gallery.Directory, "*.*", SearchOption.AllDirectories).Length; filesSoFar = 0; ftpClient.UploadDirectory(Program.Settings.Gallery.Directory, true, false); } catch (Exception exception) { throw exception; } finally { ftpClient.Close(); } visualBackgroundWorker1.ReportProgress(100, "Transfer complete"); // open in browser if(!ftpSite.OptionalWebAddress.Equals(@"http://")) { System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo.UseShellExecute = true; process.StartInfo.FileName = ftpSite.OptionalWebAddress + "/" + Program.Settings.Gallery.Name; process.Start(); } } catch (Exception exception) { visualBackgroundWorker1.ReportProgress(100, "Error: " + exception.Message); } } int numberFiles; int filesSoFar; void ftpClient_FileSendingEvent(object sender, string file) { visualBackgroundWorker1.ReportProgress((filesSoFar * 100) / numberFiles, "Sending " + file.Remove(0,Program.Settings.Gallery.Directory.Length)); filesSoFar++; } private void visualBackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { cancelButton.Enabled = false; okButton.Enabled = true; } private void okButton_Click(object sender, EventArgs e) { Close(); } } class CancelledException : Exception { } }