예제 #1
0
 public void View(FileTree fileTree)
 {
     try
     {
         string way = fileTree.Way + ((ListBoxItem)fileTree.ListBox.SelectedItem).Content.ToString();
         byte[] bytes;
         if (local.SelectedItems.Count > server.SelectedItems.Count)
         {
             using (var stream = new FileStream(way, FileMode.Open)) { bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); stream.Close(); }
         }
         else if (local.SelectedItems.Count < server.SelectedItems.Count)
         {
             bytes = client.GetFileBytes(way);
         }
         else
         {
             return;
         }
         Notepad notepad = new Notepad(bytes)
         {
             Readonly = true
         };
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #2
0
 private void OnKeyDown(object sender, KeyEventArgs e, FileTree fileTree)
 {
     if (e.Key == Key.Delete)
     {
         Delete(sender, fileTree);
     }
     else if (e.Key == Key.F5)
     {
         fileTree.Submit(copy);
     }
     else if (e.Key == Key.F6)
     {
         fileTree.Submit(move);
     }
     else if (e.Key == Key.F4)
     {
         Edit(fileTree);
     }
     else if (e.Key == Key.F3)
     {
         View(fileTree);
     }
     else if (e.Key == Key.F7)
     {
         MakeDir(fileTree);
     }
 }
예제 #3
0
 public void Delete(object sender, FileTree fileTree)
 {
     try {
         string  names   = "";
         ListBox listBox = (ListBox)sender;
         foreach (ListBoxItem item in listBox.SelectedItems)
         {
             names += "\n" + (string)item.Content;
         }
         if (MessageBox.Show(names, "Удалить файлы", MessageBoxButton.YesNo) == MessageBoxResult.No)
         {
             return;
         }
         foreach (string str in names.Split(new string[1] {
             "\n"
         }, StringSplitOptions.RemoveEmptyEntries))
         {
             fileTree.RemoveData(str);
         }
         fileTree.Refresh();
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #4
0
 public void Copy(FileTree fileTree, string way, string[] names)
 {
     try
     {
         if (fileTree == serverTree)
         {
             if (names.Length == 0)
             {
                 return;
             }
             foreach (string name in names)
             {
                 Task task = new Task(() => client.DownloadFile(way + name, localTree.Way));
                 try
                 {
                     task.Start();
                 }
                 catch (Exception e)
                 {
                     MessageBox.Show(e.Message);
                 }
                 Thread sleeper = new Thread(new ThreadStart(() =>
                 {
                     Task.WaitAll(task);
                     Dispatcher.Invoke(() => localTree.Refresh());
                 }));
                 sleeper.Start();
             }
         }
         else if (fileTree == localTree)
         {
             if (names.Length == 0)
             {
                 return;
             }
             foreach (string name in names)
             {
                 client.UploadFile(serverTree.Way + name, way + name);
             }
             serverTree.Refresh();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #5
0
 public void Edit(FileTree fileTree)
 {
     try
     {
         string way = fileTree.Way + ((ListBoxItem)fileTree.ListBox.SelectedItem).Content.ToString();
         byte[] bytes;
         if (local.SelectedItems.Count > server.SelectedItems.Count)
         {
             using (var stream = new FileStream(way, FileMode.Open)) { bytes = new byte[stream.Length]; stream.Read(bytes, 0, (int)stream.Length); stream.Close(); }
         }
         else if (local.SelectedItems.Count < server.SelectedItems.Count)
         {
             bytes = client.GetFileBytes(way);
         }
         else
         {
             return;
         }
         Notepad notepad = new Notepad(bytes)
         {
             Readonly = false
         };
         notepad.onSaveSubmit += (string text) =>
         {
             byte[] bytearr = ASCIIEncoding.ASCII.GetBytes(text);
             if (local.SelectedItems.Count > server.SelectedItems.Count)
             {
                 File.WriteAllBytes(way, bytearr);
             }
             else if (local.SelectedItems.Count < server.SelectedItems.Count)
             {
                 client.WriteFile(bytearr, way);
             }
             else
             {
                 return;
             }
         };
     }catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #6
0
 public void MakeFile(FileTree fileTree)
 {
     try
     {
         AskWindow askWindow = new AskWindow();
         askWindow.Title = "Создание файла";
         askWindow.Show();
         askWindow.name.Focus();
         askWindow.Left            = Left + Width / 2;
         askWindow.Top             = Top + Height / 2;
         askWindow.name.MouseDown += (object sender, MouseButtonEventArgs e) =>
         {
             if (askWindow.name.Text == "TextBox")
             {
                 askWindow.name.Text = "";
             }
         };
         askWindow.submit.Click += (object sender, RoutedEventArgs e) =>
         {
             string name = askWindow.name.Text;
             if (fileTree == serverTree)
             {
                 client.CreateFile(fileTree.Way + name);
             }
             else if (fileTree == localTree)
             {
                 File.Create(fileTree.Way + name);
             }
             askWindow.Close();
             fileTree.Refresh();
         };
         askWindow.cancel.Click += (object sender, RoutedEventArgs e) =>
         {
             askWindow.Close();
         };
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #7
0
 public void Move(FileTree fileTree, string way, string[] names)
 {
     try {
         if (fileTree == serverTree)
         {
             if (names.Length == 0)
             {
                 return;
             }
             foreach (string name in names)
             {
                 client.DownloadFileAsync(way + name, localTree.Way);
                 client.DeleteFile(way + name);
                 serverTree.Refresh();
                 localTree.Refresh();
             }
         }
         else if (fileTree == localTree)
         {
             if (names.Length == 0)
             {
                 return;
             }
             foreach (string name in names)
             {
                 client.UploadFile(serverTree.Way + name, way + name);
             }
             foreach (string name in names)
             {
                 File.Delete(way + name);
             }
             serverTree.Refresh();
             localTree.Refresh();
         }
     }
     catch (Exception e)
     {
         MessageBox.Show(e.Message);
     }
 }
예제 #8
0
 public void InitFileTrees()
 {
     server.PreviewMouseDown += (object sender, MouseButtonEventArgs e) => local.SelectedItems.Clear();
     local.PreviewMouseDown  += (object sender, MouseButtonEventArgs e) => server.SelectedItems.Clear();
     localTree          = new FileTree(local, localDir);
     localTree.TextBox  = localWay;
     local.KeyDown     += (object sender, KeyEventArgs e) => OnKeyDown(sender, e, localTree);
     client             = new FTPClient();
     client.Url         = ip;
     client.Login       = login;
     client.Password    = password;
     client.onError    += (object sender, Exception e) => MessageBox.Show(e.Message);
     serverTree         = new FileTree(server, serverDir, client);
     serverTree.TextBox = serverWay;
     server.KeyDown    += (object sender, KeyEventArgs e) => OnKeyDown(sender, e, serverTree);
     serverTree.AddSubmitButton(copy);
     serverTree.AddSubmitButton(move);
     localTree.AddSubmitButton(copy);
     localTree.AddSubmitButton(move);
     localWay.KeyDown += (object sender, KeyEventArgs e) =>
     {
         if (e.Key != Key.Enter)
         {
             return;
         }
         if (!Directory.Exists(localWay.Text))
         {
             MessageBox.Show("Incorrect way: " + localWay.Text, "Error");
             localWay.Text = localTree.Way;
         }
         if (localWay.Text[localWay.Text.Length - 1] != '\\')
         {
             localWay.Text += @"\";
         }
         localTree.Way = localWay.Text;
         localTree.Refresh();
         localWay.CaretIndex = localWay.Text.Length;
     };
     serverWay.KeyDown += (object sender, KeyEventArgs e) =>
     {
         if (e.Key != Key.Enter)
         {
             return;
         }
         string w = serverWay.Text;
         if (w.Contains('\\'))
         {
             w = w.Substring(0, serverWay.Text.LastIndexOf(@"\"));
         }
         if (w.Contains('\\'))
         {
             w = w.Substring(0, w.LastIndexOf(@"\"));
         }
         string mask = serverWay.Text;
         if (mask[mask.Length - 1] == '\\')
         {
             mask = mask.Substring(0, mask.Length - 1);
         }
         if (mask.Contains('\\'))
         {
             mask = mask.Substring(mask.LastIndexOf(@"\") + 1);
         }
         if (!(client.GetListDirectory(w).Contains(mask) || (w == mask)))
         {
             MessageBox.Show("Incorrect way: " + serverWay.Text, "Error");
             serverWay.Text = serverTree.Way;
         }
         if (serverWay.Text[serverWay.Text.Length - 1] != '\\')
         {
             serverWay.Text += @"\";
         }
         serverTree.Way = serverWay.Text;
         serverTree.Refresh();
         serverWay.CaretIndex = serverWay.Text.Length;
     };
     serverTree.onFileSelect += (object sender, FileTreeEventArgs e) =>
     {
         if (e.Button == copy)
         {
             Copy((FileTree)sender, e.Way, e.Names);
         }
         if (e.Button == move)
         {
             Move((FileTree)sender, e.Way, e.Names);
         }
     };
     localTree.onFileSelect += (object sender, FileTreeEventArgs e) =>
     {
         if (e.Button == copy)
         {
             Copy((FileTree)sender, e.Way, e.Names);
         }
         if (e.Button == move)
         {
             Move((FileTree)sender, e.Way, e.Names);
         }
     };
 }