20240301_JSEQ_upperpc/JiangsuEarthquakeNow/JiangsuEarthquake/Views/UserControls/FTPSettingView.xaml.cs
XuMin 3b6c570800 1 经过电控腔测试和升压站测试;
2 解决了后一个通信连接会影响前一个通信连接的问题;
3 测试过程中存在的问题修改;
2024-09-03 16:30:34 +08:00

587 lines
22 KiB
C#

using _3DTools;
using JiangsuEarthquake.Common;
using JiangsuEarthquake.Models.FTP;
using JiangsuEarthquake.ViewModels;
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.Forms;
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 System.Windows.Threading;
using static MaterialDesignThemes.Wpf.Theme;
using MessageBox = System.Windows.MessageBox;
using UserControl = System.Windows.Controls.UserControl;
using HandyControl.Tools.Extension;
using System.Collections.ObjectModel;
namespace JiangsuEarthquake.Views.UserControls
{
/// <summary>
/// FTPSettingView.xaml 的交互逻辑
/// </summary>
public partial class FTPSettingView : UserControl
{
public FTPSettingView(int id)
{
InitializeComponent();
stationId = id;
this.DataContext = this;
ReturnHighFTPBtn.IsEnabled = false;//禁用返回上一级菜单按钮
ReturnHighLoaclBtn.IsEnabled = false;//禁用返回上一级菜单按钮
if (stationId == 1)
{
ftpcon.IP = Tools.GetAppSetting("FTPIP1");
ftpcon.Port = int.Parse(Tools.GetAppSetting("FTPPort1"));
ftpcon.UserName = Tools.GetAppSetting("FTPUserName1");
ftpcon.PassWord = Tools.GetAppSetting("FTPPassword1");
}
else
{
ftpcon.IP = Tools.GetAppSetting("FTPIP2");
ftpcon.Port = int.Parse(Tools.GetAppSetting("FTPPort2"));
ftpcon.UserName = Tools.GetAppSetting("FTPUserName2");
ftpcon.PassWord = Tools.GetAppSetting("FTPPassword2");
}
RefreshLocalList(@"F:\");
//RefreshFTPList("FTP_Test");
timerDownloadFileMsgHidden.Interval = TimeSpan.FromSeconds(2);
timerDownloadFileMsgHidden.Tick += TimerDownloadFileMsgHidden_Tick;
timerUploadFileMsgHidden.Interval = TimeSpan.FromSeconds(2);
timerUploadFileMsgHidden.Tick += TimerUploadFileMsgHidden_Tick;
}
int stationId = 1;
public FTPLIST ftpList { get; set; } = new FTPLIST();
public FTPConnectModel ftpcon { get; set; } = new FTPConnectModel();
#region
public DispatcherTimer timerDownloadFileMsgHidden = new DispatcherTimer();
public DispatcherTimer timerUploadFileMsgHidden = new DispatcherTimer();
private void TimerDownloadFileMsgHidden_Tick(object sender, EventArgs e)
{
DownloadFileMsg.Visibility = Visibility.Hidden;
// 停止定时器
(sender as DispatcherTimer).Stop();
}
private void DownloadFileBtn_Click(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (FTPFloderDataGrid.SelectedItems.Count == 0)
{
DownloadFileMsg.Visibility = Visibility.Visible;
DownloadFileMsg.Text = "请选择文件!";
DownloadFileMsg.Foreground = new SolidColorBrush(Colors.Red);
timerDownloadFileMsgHidden.Start();
return;
}
foreach (FTPModel ft in FTPFloderDataGrid.SelectedItems)
{
if (!System.IO.File.Exists(ftpList.datagrid2Path + ft.FileName))
{
ftpcon.Download(ftpList.datagrid2Path, ft.FileName);
RefreshLocalList(ftpList.datagrid2Path);
DownloadFileMsg.Visibility = Visibility.Visible;
DownloadFileMsg.Text = "文件下载成功";
DownloadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerDownloadFileMsgHidden.Start();
}
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);
DownloadFileMsg.Visibility = Visibility.Visible;
DownloadFileMsg.Text = "文件下载成功";
DownloadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerDownloadFileMsgHidden.Start();
break;
case MessageBoxResult.No:
return;
}
}
}
}
private void TimerUploadFileMsgHidden_Tick(object sender, EventArgs e)
{
UploadFileMsg.Visibility = Visibility.Hidden;
// 停止定时器
(sender as DispatcherTimer).Stop();
}
private void UploadFileBtn_Click(object sender, RoutedEventArgs e)
{
if (LocalFloderDataGrid.SelectedItems.Count == 0)
{
UploadFileMsg.Visibility = Visibility.Visible;
UploadFileMsg.Text = "请选择文件!";
UploadFileMsg.Foreground = new SolidColorBrush(Colors.Red);
timerUploadFileMsgHidden.Start();
return;
}
foreach (FTPModel item in LocalFloderDataGrid.SelectedItems)
{
//首先判断文件类型是否为文件夹
if (item.IsDirectory)
{
if (!ftpcon.IsDirectoryExist(item.FileName)) //文件夹不存在
{
ftpcon.Upload(item.FilePath); //上传
RefreshFTPList(ftpList.datagrid1Path); //刷新
UploadFileMsg.Visibility = Visibility.Visible;
UploadFileMsg.Text = "文件上传成功";
UploadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerUploadFileMsgHidden.Start();
}
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);
UploadFileMsg.Visibility = Visibility.Visible;
UploadFileMsg.Text = "文件上传成功";
UploadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerUploadFileMsgHidden.Start();
break;
case MessageBoxResult.No:
return;
}
}
}
else
{
if (!ftpcon.IsFileExist(item.FileName)) //如果文件不存在
{
ftpcon.Upload(item.FilePath); //上传
RefreshFTPList(ftpList.datagrid1Path); //刷新
UploadFileMsg.Visibility = Visibility.Visible;
UploadFileMsg.Text = "文件上传成功";
UploadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerUploadFileMsgHidden.Start();
}
else //文件存在
{
MessageBoxResult result = MessageBox.Show("文件已存在,是否覆盖?", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
//先删除原文件
ftpcon.DeleteFile(item.FileName);
//再将新文件拷贝进来
ftpcon.Upload(item.FilePath); //上传文件
//刷新一下界面
RefreshFTPList(ftpList.datagrid1Path);
UploadFileMsg.Visibility = Visibility.Visible;
UploadFileMsg.Text = "文件上传成功";
UploadFileMsg.Foreground = new SolidColorBrush(Colors.Green);
timerUploadFileMsgHidden.Start();
break;
case MessageBoxResult.No:
return;
}
}
}
}
}
#endregion
#region
/// <summary>
/// 更新本地文件目录
/// </summary>
/// <param name="LocalPath"></param>
public void RefreshLocalList(string LocalPath)
{
try
{
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;
}
catch (Exception ex)
{
}
}
/// <summary>
/// 更新FTP文件目录
/// </summary>
/// <param name="FTPPath"></param>
public void RefreshFTPList(string FTPPath)
{
try
{
string ftpURL = ftpcon.getFtpHelper(ftpcon.IP, ftpcon.Port, FTPPath, ftpcon.UserName, ftpcon.PassWord, "");
List<FTPModel> list = ftpcon.ListDirectories(); //该目录下的文件夹
List<FTPModel> File_list = ftpcon.ListFiles(); //该目录下的文件
if (File_list != null)
{
for (int i = 0; i < File_list.Count; i++)
{
list.Add(File_list[i]);
}
}
ftpList.FTPList = list;
}
catch (Exception ex)
{
}
}
#endregion
#region FTP目录操作
/// <summary>
/// FTP目录文件夹双击打开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FTPFloderDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (FTPFloderDataGrid.SelectedItem == null)
return;
ftpList.datagrid1Path += ((FTPModel)FTPFloderDataGrid.SelectedItem).FileName + "/";
RefreshFTPList(ftpList.datagrid1Path);
ReturnHighFTPBtn.IsEnabled = true;
}
/// <summary>
/// FTP目录文件夹返回上级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReturnHighFTPBtn_Click(object sender, RoutedEventArgs e)
{
if (Regex.Matches(ftpList.datagrid1Path, "/").Count == 1)
{
ftpList.datagrid1Path = "";
ReturnHighFTPBtn.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 RefreshFTPBtn_Click(object sender, RoutedEventArgs e)
{
RefreshFTPList(ftpList.datagrid1Path);
}
/// <summary>
/// FTP目录文件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DeleteFTPBtn_Click(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (FTPFloderDataGrid.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 FTPFloderDataGrid.SelectedItems)
{
//首先判断需要删除的文件类型
if (item.IsDirectory) //如果是文件目录
{
ftpcon.RemoveDirectory(item.FilePath);
if (FTPFloderDataGrid.SelectedItems.Count == 0)
break;
}
else //选中的内容是文件
{
ftpcon.DeleteFile(item.FileName);
if (FTPFloderDataGrid.SelectedItems.Count == 0)
break;
}
}
RefreshFTPList(ftpList.datagrid1Path);
break;
case MessageBoxResult.No:
return;
}
}
/// <summary>
/// 选择FTP文件夹目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectFTPPathBtn_Click(object sender, RoutedEventArgs e)
{
ftpList.datagrid1Path = "FTP_Test";
//ftpcon.ftpURI = ftpList.datagrid1Path;
}
/// <summary>
/// 确认FTP文件夹目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EnsureFTPPathBtn_Click(object sender, RoutedEventArgs e)
{
RefreshFTPList(ftpList.datagrid1Path);
}
#endregion
#region
/// <summary>
/// 本地目录文件夹双击打开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LocalFloderDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (LocalFloderDataGrid.SelectedItem == null)
return;
if (!((FTPModel)LocalFloderDataGrid.SelectedItem).IsDirectory)
return;
RefreshLocalList(((FTPModel)LocalFloderDataGrid.SelectedItem).FilePath + "\\");
ReturnHighLoaclBtn.IsEnabled = true;
}
/// <summary>
/// 本地目录文件及返回上级
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ReturnHighLoaclBtn_Click(object sender, RoutedEventArgs e)
{
if (Regex.Matches(ftpList.datagrid2Path, @"\\").Count == 1)
{
ReturnHighLoaclBtn.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 RefreshLocalBtn_Click(object sender, RoutedEventArgs e)
{
RefreshLocalList(ftpList.datagrid2Path);
}
/// <summary>
/// 本地目录文件删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DeleteLocalBtn_Click(object sender, RoutedEventArgs e)
{
//首先判断是否有选中文件
if (LocalFloderDataGrid.SelectedItems.Count == 0)
{
MessageBox.Show("请选择需要删除的文件或文件夹!");
return;
}
MessageBoxResult result = MessageBox.Show("请确认是否删除选中的文件!", "警告!", MessageBoxButton.YesNo);
switch (result)
{
case MessageBoxResult.Yes:
foreach (FTPModel item in LocalFloderDataGrid.SelectedItems)
{
//首先判断需要删除的文件类型
if (item.IsDirectory) //如果是文件目录
{
File.Delete(item.FilePath);
if (LocalFloderDataGrid.SelectedItems.Count == 0)
break;
}
else //选中的内容是文件
{
File.Delete(item.FilePath);
if (LocalFloderDataGrid.SelectedItems.Count == 0)
break;
}
}
RefreshLocalList(ftpList.datagrid2Path);
break;
case MessageBoxResult.No:
return;
}
}
/// <summary>
/// 选择本地文件夹目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectLocalPathBtn_Click(object sender, RoutedEventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.Description = "请选择文件夹";
DialogResult result = dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string folderPath = dialog.SelectedPath;
ftpList.datagrid2Path = folderPath;
// 使用获取到的文件夹路径
MessageBox.Show("选择的文件夹路径是:" + folderPath);
}
}
}
/// <summary>
/// 确认本地文件夹目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EnsureLocalPathBtn_Click(object sender, RoutedEventArgs e)
{
RefreshLocalList(ftpList.datagrid2Path);
}
#endregion
private void ConnectSetBtn_Click(object sender, RoutedEventArgs e)
{
if (stationId == 1)
HandyControl.Controls.Dialog.Show(new FTPConnectSstView(1));
else
HandyControl.Controls.Dialog.Show(new FTPConnectSstView(2));
}
private void FTPFloderDataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if ( e.Column.Header.ToString() == "Index" || e.Column.Header.ToString() == "FilePath" || e.Column.Header.ToString() == "IsDirectory")
{
e.Cancel = true;
}
}
}
}