20220510_191_upperpc/FTP/FTPControlWindow.xaml.cs
2023-07-27 10:57:34 +08:00

500 lines
18 KiB
C#

using FTP.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Net.WebRequestMethods;
using File = System.IO.File;
namespace FTP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class FTPControlWindow : Window
{
public FTPLIST ftpList { get; set; } = new FTPLIST();
public FTPConnectModel ftpcon { get; set; } = new FTPConnectModel();
FilesHelper filesHelper = new FilesHelper();
public FTPControlWindow()
{
//RefreshLocalList(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);//获取当前程序运行路径
RefreshLocalList(@"D:\");//获取当前程序运行路径
InitializeComponent();
this.DataContext = this;
retrunNext.IsEnabled = false;//禁用返回上一级菜单按钮
retrunNextLocal.IsEnabled = false;//禁用返回上一级菜单按钮
ftpcon.IP = "10.10.10.5";
ftpcon.Port = 21;
ftpcon.UserName = "Chen";
ftpcon.PassWord = "Acwy@700715";
}
public FTPControlWindow(string IP, int Port, string UserName, string PassWord)
{
ftpcon.IP = IP;
ftpcon.Port = Port;
ftpcon.UserName = UserName;
ftpcon.PassWord = PassWord;
RefreshLocalList(@"D:\");//获取当前程序运行路径
InitializeComponent();
this.DataContext = this;
retrunNext.IsEnabled = false;//禁用返回上一级菜单按钮
retrunNextLocal.IsEnabled = false;//禁用返回上一级菜单按钮
}
/// <summary>
/// 文件导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (datagrid1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件!");
return;
}
foreach (FTPModel ft in datagrid1.SelectedItems)
{
if (!System.IO.File.Exists(ftpList.datagrid2Path + ft.FileName))
{
ftpcon.Download(ftpList.datagrid2Path, ft.FileName);
RefreshLocalList(ftpList.datagrid2Path);
}
else
{
MessageBoxResult result = MessageBox.Show("文件已存在,是否覆盖?", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
//先删除原文件
File.Delete(ftpList.datagrid2Path + ft.FileName);
//再将新文件拷贝进来
ftpcon.Download(ftpList.datagrid2Path, ft.FileName);
RefreshLocalList(ftpList.datagrid2Path);
break;
case MessageBoxResult.No:
return;
break;
}
}
}
}
/// <summary>
/// 文件导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (datagrid2.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件!");
return;
}
foreach (FTPModel item in datagrid2.SelectedItems)
{
//首先判断文件类型是否为文件夹
if (item.IsDirectory)
{
if (!ftpcon.IsDirectoryExist(item.FileName)) //文件夹不存在
{
ftpcon.Upload(item.FilePath); //上传
RefreshFTPList(ftpList.datagrid1Path); //刷新
}
else //文件夹存在
{
MessageBoxResult result = MessageBox.Show("文件夹已存在,是否覆盖?", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
//先删除原文件
ftpcon.RemoveDirectory(ftpcon.ftpRemotePath + "/" + item.FileName);
//再将新文件拷贝进来
ftpcon.Upload(item.FilePath); //上传文件夹
//刷新一下界面
RefreshFTPList(ftpList.datagrid1Path);
break;
case MessageBoxResult.No:
return;
break;
}
}
}
else
{
if (!ftpcon.IsFileExist(item.FileName)) //如果文件不存在
{
ftpcon.Upload(item.FilePath); //上传
RefreshFTPList(ftpList.datagrid1Path); //刷新
}
else //文件存在
{
MessageBoxResult result = MessageBox.Show("文件已存在,是否覆盖?", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
//先删除原文件
ftpcon.DeleteFile(item.FileName);
//再将新文件拷贝进来
ftpcon.Upload(item.FilePath); //上传文件
//刷新一下界面
RefreshFTPList(ftpList.datagrid1Path);
break;
case MessageBoxResult.No:
return;
break;
}
}
}
}
ftpcon.Upload(((FTPModel)datagrid2.SelectedItem).FilePath);
}
/// <summary>
/// 设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_3(object sender, RoutedEventArgs e)
{
}
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_4(object sender, RoutedEventArgs e)
{
RefreshFTPList(ftpList.datagrid1Path);
}
/// <summary>
/// 断开连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_5(object sender, RoutedEventArgs e)
{
ftpList.LocalList.Clear();
}
/// <summary>
/// datagrid1双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void datagrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ftpList.datagrid1Path += ((FTPModel)datagrid1.SelectedItem).FileName + "/";
RefreshFTPList(ftpList.datagrid1Path);
retrunNext.IsEnabled = true;
}
/// <summary>
/// 本地文件目录双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void datagrid2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
FTPModel ftpModel = (FTPModel)datagrid2.SelectedItem;
if (!((FTPModel)datagrid2.SelectedItem).IsDirectory)
return;
RefreshLocalList(((FTPModel)datagrid2.SelectedItem).FilePath + "\\");
retrunNextLocal.IsEnabled = true;
}
/// <summary>
/// FTP返回上一级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_6(object sender, RoutedEventArgs e)
{
if (Regex.Matches(ftpList.datagrid1Path, "/").Count == 1)
{
ftpList.datagrid1Path = "";
retrunNext.IsEnabled = false;
}
else
{
int wz = 0;
for (int i = 0; i < ftpList.datagrid1Path.Length - 1; i++)
{
if (ftpList.datagrid1Path.Substring(i, 1).Equals("/"))
{
wz = i;
}
}
ftpList.datagrid1Path = ftpList.datagrid1Path.Remove(wz + 1, ftpList.datagrid1Path.Length - wz - 1);
}
RefreshFTPList(ftpList.datagrid1Path);
}
/// <summary>
/// FTP文件刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void refresh_Click(object sender, RoutedEventArgs e)
{
RefreshFTPList(ftpList.datagrid1Path);
}
/// <summary>
/// FTP文件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void delete_Click(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (datagrid1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择需要删除的文件或文件夹!");
return;
}
MessageBoxResult result = MessageBox.Show("请确认是否删除选中的文件!", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
List<FTPModel> fTPModels = new List<FTPModel>();
foreach (FTPModel item in datagrid1.SelectedItems)
{
//首先判断需要删除的文件类型
if (item.IsDirectory) //如果是文件目录
{
ftpcon.RemoveDirectory(item.FilePath);
if(datagrid1.SelectedItems.Count == 0)
break;
}
else //选中的内容是文件
{
ftpcon.DeleteFile(item.FileName);
if (datagrid1.SelectedItems.Count == 0)
break;
}
}
RefreshFTPList(ftpList.datagrid1Path);
break;
case MessageBoxResult.No:
return;
break;
}
}
/// <summary>
/// 本地文件返回上一级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void retrunNextLocal_Click(object sender, RoutedEventArgs e)
{
if (Regex.Matches(ftpList.datagrid2Path, @"\\").Count == 1)
{
retrunNext.IsEnabled = false;
}
else
{
int wz = 0;
for (int i = 0; i < ftpList.datagrid2Path.Length - 1; i++)
{
if (ftpList.datagrid2Path.Substring(i, 1).Equals("\\"))
{
wz = i;
}
}
ftpList.datagrid2Path = ftpList.datagrid2Path.Remove(wz + 1, ftpList.datagrid2Path.Length - wz - 1);
RefreshLocalList(ftpList.datagrid2Path);
}
}
/// <summary>
/// 本地文件刷新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void refreshLocal_Click(object sender, RoutedEventArgs e)
{
RefreshLocalList(ftpList.datagrid2Path);
}
/// <summary>
/// 本地文件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void deleteLocal_Click(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (datagrid2.SelectedItems.Count == 0)
{
MessageBox.Show("请选择需要删除的文件或文件夹!");
return;
}
MessageBoxResult result = MessageBox.Show("请确认是否删除选中的文件!", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
foreach (FTPModel item in datagrid2.SelectedItems)
{
//首先判断需要删除的文件类型
if (item.IsDirectory) //如果是文件目录
{
File.Delete(item.FilePath);
if (datagrid2.SelectedItems.Count == 0)
break;
}
else //选中的内容是文件
{
File.Delete(item.FilePath);
if (datagrid2.SelectedItems.Count == 0)
break;
}
}
RefreshLocalList(ftpList.datagrid2Path);
break;
case MessageBoxResult.No:
return;
break;
}
}
/// <summary>
/// 更新本地文件目录
/// </summary>
/// <param name="LocalPath"></param>
public void RefreshLocalList(string LocalPath)
{
ftpList.datagrid2Path = LocalPath;
string[] localfiles = Directory.GetFiles(ftpList.datagrid2Path); //获取文件名称
string[] localdir = Directory.GetDirectories(ftpList.datagrid2Path); //获取目录名称
List<FTPModel> fs = new List<FTPModel>();
for (int i = 0; i < localfiles.Length; i++)
{
fs.Add(new FTPModel()
{
FileName = localfiles[i].Split('\\')[localfiles[i].Split('\\').Length - 1],
CreateTime = Directory.GetLastAccessTime(localfiles[i]),
FileSize = FilesHelper.GetFileSize(localfiles[i]),
IsDirectory = false,
FilePath = localfiles[i]
});
}
for (int i = 0; i < localdir.Length; i++)
{
fs.Add(new FTPModel()
{
FileName = localdir[i].Split('\\')[localdir[i].Split('\\').Length - 1],
CreateTime = Directory.GetLastAccessTime(localdir[i]),
FileSize = "",
IsDirectory = true,
FilePath = localdir[i]
});
}
ftpList.LocalList = fs;
}
public void RefreshFTPList(string FTPPath)
{
string ftpURL = ftpcon.getFtpHelper(ftpcon.IP, ftpcon.Port, FTPPath, ftpcon.UserName, ftpcon.PassWord, "");
List<FTPModel> list = ftpcon.ListDirectories(); //该目录下的文件夹
List<FTPModel> File_list = ftpcon.ListFiles(); //该目录下的文件
for (int i = 0; i < File_list.Count; i++)
{
list.Add(File_list[i]);
}
ftpList.FTPList = list;
}
/// <summary>
/// 主窗口关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
/// <summary>
/// 最大化按钮
/// </summary>
private void MaxButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
/// <summary>
/// 最小化按钮
/// </summary>
private void MinButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
/// <summary>
/// 顶部蓝色区域按钮按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
switch (e.ClickCount)
{
case 2:
{
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
break;
}
}
}
}
}