private void fileList_Drop(object sender, DragEventArgs e) { try { FTPLibrary ftpLib = new FTPLibrary(userNameBox.Text, passwordBox.Password, isSecureConnection); int count = 0; string log = ""; if (e.Data is System.Windows.DataObject && ((System.Windows.DataObject)e.Data).ContainsFileDropList()) { foreach (string filePath in ((System.Windows.DataObject)e.Data).GetFileDropList()) { double size = ftpLib.GetFTPSize(ftpBox.Text + "/" + System.IO.Path.GetFileName(filePath)); if (size > 0) { MessageBoxResult r; r = MessageBox.Show("The file: '" + System.IO.Path.GetFileName(filePath) + "' already exist on this server, do you want to overwrite?", "File exist", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No); if (r == MessageBoxResult.Yes) { log = ftpLib.Upload(ftpBox.Text + "/" + System.IO.Path.GetFileName(filePath), filePath); count++; } } else { log = ftpLib.Upload(ftpBox.Text + "/" + System.IO.Path.GetFileName(filePath), filePath); fileList.Items.Add(System.IO.Path.GetFileName(filePath)); count++; } } } if (log.StartsWith("Error")) ShowStatus(log); else ShowStatus("Succesfully uploaded " + count + " files at '" + ftpBox.Text + "'"); } catch (Exception ex){ ShowStatus(ex.Message); } }
//Save files in the background private void worker_DoWork(object sender, DoWorkEventArgs e) { //Get arguments string[] tmp = (e.Argument as string[]); //Attempt to save file try { //Save the file StreamWriter w = new StreamWriter(tmp[0]); w.Write(tmp[1]); w.Close(); } catch { //Set result status code on failure e.Result = "Error! Unable to save file: " + System.IO.Path.GetFileName(tmp[0]); } try { //Save the file if (tmp[2] == "true") { //Attempts to upload file string check = ""; this.Dispatcher.Invoke((Action)(() => { FTPLibrary ftpLib = new FTPLibrary(userNameBox.Text, passwordBox.Password, isSecureConnection); check = ftpLib.Upload("ftp://" + tmp[3], tmp[0]); })); //Set result status code if (!check.StartsWith("Error")) e.Result = "Document published: (" + System.IO.Path.GetFileName(tmp[0]) + ")"; else { e.Result = "Failed to publish document! Reason: (" + check.Substring(7) + ")"; } } else { //Set result status code e.Result = "Document saved: (" + System.IO.Path.GetFileName(tmp[0]) + ")"; } } catch (Exception ex){ //Set result status code on failure e.Result = "Error! Unable to publish file: " + System.IO.Path.GetFileName(tmp[0]) + ", Reason: (" + ex.Message + ")"; } }
private string[] DownloadFTPFiles(bool loadToEditor = true) { try { //Check if items are selected if (fileList.SelectedItems.Count > 0) { //Create directory if doesn't exist string downloadDir = webFilesPath + ftpBox.Text.Replace("ftp://", "").Replace("/", "\\"); if (!Directory.Exists(downloadDir)) { Directory.CreateDirectory(downloadDir); } //Download FTP files List<string> filesToLoad = new List<string>(); FTPLibrary ftpLib = new FTPLibrary(userNameBox.Text, passwordBox.Password, isSecureConnection); for (int i = 0; i < fileList.SelectedItems.Count; i++) { // Make backup if already exist (conflict) if (File.Exists(downloadDir + "\\" + fileList.SelectedItems[i].ToString())) { if (File.Exists(downloadDir + "\\" + fileList.SelectedItems[i].ToString() + "~")) File.Delete(downloadDir + "\\" + fileList.SelectedItems[i].ToString() + "~"); File.Move(downloadDir + "\\" + fileList.SelectedItems[i].ToString(), downloadDir + "\\" + fileList.SelectedItems[i].ToString() + "~"); } //Download FTP file ftpLib.Download(ftpBox.Text + "\\" + fileList.SelectedItems[i].ToString(), downloadDir + "\\" + fileList.SelectedItems[i].ToString()); filesToLoad.Add(downloadDir + "\\" + fileList.SelectedItems[i].ToString()); } //Load file into editor if (loadToEditor) Load(filesToLoad.ToArray()); //Return file list return filesToLoad.ToArray(); } return null; } catch (Exception ex) { ShowStatus(ex.Message); return null; } }
//Window key down event private void Window_KeyDown(object sender, KeyEventArgs e) { if ((Keyboard.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0) { //Default key combinations if ((Keyboard.GetKeyStates(Key.N) & KeyStates.Down) > 0) AddDocument(""); if ((Keyboard.GetKeyStates(Key.O) & KeyStates.Down) > 0) Load(); if ((Keyboard.GetKeyStates(Key.S) & KeyStates.Down) > 0) Save(); if ((Keyboard.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0) { if ((Keyboard.GetKeyStates(Key.S) & KeyStates.Down) > 0) { SaveAll(); } } if ((Keyboard.GetKeyStates(Key.U) & KeyStates.Down) > 0) Publish(); if ((Keyboard.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0) { if ((Keyboard.GetKeyStates(Key.U) & KeyStates.Down) > 0) { PublishAll(); } } if ((Keyboard.GetKeyStates(Key.F) & KeyStates.Down) > 0) { if (grid_find.Visibility == System.Windows.Visibility.Visible) HideFindReplace(); else ShowFind(); } if ((Keyboard.GetKeyStates(Key.R) & KeyStates.Down) > 0) { if (grid_replace.Visibility == System.Windows.Visibility.Visible) HideFindReplace(); else ShowReplace(); } } if ((Keyboard.GetKeyStates(Key.Enter) & KeyStates.Down) > 0) { if (grid_find.Visibility == System.Windows.Visibility.Visible) FindInDocument(); else if (passwordBox.IsKeyboardFocused || userNameBox.IsKeyboardFocused || ftpBox.IsFocused) { //Connect to FTP server SetupFTP(); } } if ((Keyboard.GetKeyStates(Key.Delete) & KeyStates.Down) > 0) { if (fileList.SelectedIndex > -1) { MessageBoxResult r = MessageBox.Show("Are you sure you wish to delete these files?", "Confirm delete", MessageBoxButton.YesNo, MessageBoxImage.Question); //Delete selected files if (r == MessageBoxResult.Yes) { FTPLibrary lib = new FTPLibrary(userNameBox.Text, passwordBox.Password, isSecureConnection); string res = lib.DeleteFTPDirectory(ftpBox.Text + "/" + fileList.SelectedItem.ToString()); MessageBox.Show(res); } } } }