|
|
@@ -19,6 +19,7 @@ using System.Windows.Forms;
|
|
|
using UAS_Tools_HY;
|
|
|
using UAS_Tools_HY.Properties;
|
|
|
using UAS_Tools_HY.PublicMethods;
|
|
|
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
using Application = Microsoft.Office.Interop.Excel.Application;
|
|
|
using DataTable = System.Data.DataTable;
|
|
|
|
|
|
@@ -41,6 +42,7 @@ namespace UAS_MES_Tools
|
|
|
|
|
|
string saveFiles;
|
|
|
|
|
|
+ int currentNum = 1;
|
|
|
public Main(string account, string password)
|
|
|
{
|
|
|
_Account = account;
|
|
|
@@ -87,11 +89,11 @@ namespace UAS_MES_Tools
|
|
|
Directory.CreateDirectory(saveFiles);
|
|
|
}
|
|
|
|
|
|
- saveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
|
|
|
- if (!Directory.Exists(saveFiles))
|
|
|
- {
|
|
|
- Directory.CreateDirectory(saveFiles);
|
|
|
- }
|
|
|
+ //saveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
|
|
|
+ //if (!Directory.Exists(saveFiles))
|
|
|
+ //{
|
|
|
+ // Directory.CreateDirectory(saveFiles);
|
|
|
+ //}
|
|
|
|
|
|
File_timer = new Timer();
|
|
|
File_timer.Interval = 30 * 1000;
|
|
|
@@ -99,7 +101,7 @@ namespace UAS_MES_Tools
|
|
|
|
|
|
timerVal.Text = "30";
|
|
|
FileCountVal.Text = "10";
|
|
|
- FtpPathVal.Text = "ftp://10.18.6.200:21//2-各部门资料夹//23-信息部//dcr";
|
|
|
+ FtpPathVal.Text = "ftp://10.18.13.48:21";
|
|
|
}
|
|
|
|
|
|
private void SN_KeyDown(object sender, KeyEventArgs e)
|
|
|
@@ -747,6 +749,7 @@ namespace UAS_MES_Tools
|
|
|
FtpPwVal.Enabled = false;
|
|
|
timerVal.Enabled = false;
|
|
|
FtpPathVal.Enabled = false;
|
|
|
+ FileCountVal.Enabled = false;
|
|
|
FtpConnect.Text = "关闭连接";
|
|
|
FtpListen.Enabled = true;
|
|
|
}
|
|
|
@@ -762,6 +765,7 @@ namespace UAS_MES_Tools
|
|
|
FtpPwVal.Enabled = true;
|
|
|
timerVal.Enabled = true;
|
|
|
FtpPathVal.Enabled = true;
|
|
|
+ FileCountVal.Enabled = true;
|
|
|
FtpConnect.Text = "开启连接";
|
|
|
FtpListen.Enabled = false;
|
|
|
}
|
|
|
@@ -866,10 +870,12 @@ namespace UAS_MES_Tools
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (!Directory.Exists(saveFiles))
|
|
|
+ string childSaveFiles = Path.Combine(saveFiles, DateTime.Now.ToString("yyyyMMdd"));
|
|
|
+ if (!Directory.Exists(childSaveFiles))
|
|
|
{
|
|
|
- Directory.CreateDirectory(saveFiles);
|
|
|
- LogMessage($"创建本地文件夹: {saveFiles}");
|
|
|
+ Directory.CreateDirectory(childSaveFiles);
|
|
|
+ currentNum = 1;
|
|
|
+ LogMessage($"创建本地文件夹: {childSaveFiles}");
|
|
|
}
|
|
|
|
|
|
string ftpServer = FtpPathVal.Text.Trim();
|
|
|
@@ -892,8 +898,13 @@ namespace UAS_MES_Tools
|
|
|
|
|
|
foreach (string remoteFile in fileList)
|
|
|
{
|
|
|
+ if (!Directory.Exists(childSaveFiles))
|
|
|
+ {
|
|
|
+ Directory.CreateDirectory(childSaveFiles);
|
|
|
+ }
|
|
|
+
|
|
|
string remoteFilePath = $"{remoteFolderPath.TrimEnd('/')}/{remoteFile}";
|
|
|
- string localFilePath = Path.Combine(saveFiles, remoteFile);
|
|
|
+ string localFilePath = Path.Combine(childSaveFiles, remoteFile);
|
|
|
|
|
|
if (DownloadSingleFile(ftpServer, remoteFilePath, localFilePath, username, password))
|
|
|
{
|
|
|
@@ -903,19 +914,17 @@ namespace UAS_MES_Tools
|
|
|
{
|
|
|
failCount++;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- LogMessage($"下载完成: 成功 {successCount} 个, 失败 {failCount} 个");
|
|
|
-
|
|
|
- string[] files = Directory.GetFiles(saveFiles);
|
|
|
- string zipFilePath = Path.Combine(Path.GetDirectoryName(saveFiles),Path.GetFileName(saveFiles) + ".zip");
|
|
|
|
|
|
- if (Convert.ToInt32(FileCountVal.Text.Trim()) == files.Length)
|
|
|
- {
|
|
|
- CompressFolder(saveFiles, zipFilePath);
|
|
|
+ string[] files = Directory.GetFiles(childSaveFiles);
|
|
|
+ string zipFilePath = Path.Combine(Path.GetDirectoryName(childSaveFiles), Path.GetFileName(childSaveFiles) + $"-{currentNum}.zip");
|
|
|
+ if (Convert.ToInt32(FileCountVal.Text.Trim()) == files.Length)
|
|
|
+ {
|
|
|
+ CompressFolder(childSaveFiles, zipFilePath);
|
|
|
+ currentNum++;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return;
|
|
|
+ LogMessage($"下载完成: 成功 {successCount} 个, 失败 {failCount} 个");
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -946,17 +955,18 @@ namespace UAS_MES_Tools
|
|
|
string line;
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
{
|
|
|
- // 解析FTP返回的行,提取文件名
|
|
|
- // FTP返回格式通常类似于: "-rw-r--r-- 1 ftp ftp 12345 Jan 1 10:00 filename.txt"
|
|
|
if (!string.IsNullOrWhiteSpace(line))
|
|
|
{
|
|
|
string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
- if (parts.Length >= 9)
|
|
|
+ if (parts.Length >= 3)
|
|
|
{
|
|
|
- string fileName = parts[8];
|
|
|
-
|
|
|
- // 目录则跳过(通常以'd'开头)
|
|
|
- if (!parts[0].StartsWith("d") && fileName != "." && fileName != "..")
|
|
|
+ //string fileName = parts[8];
|
|
|
+ //if (!parts[0].StartsWith("d") && fileName != "." && fileName != "..")
|
|
|
+ //{
|
|
|
+ // fileList.Add(fileName);
|
|
|
+ //}
|
|
|
+ string fileName = parts[parts.Length - 1];
|
|
|
+ if (fileName.Contains("."))
|
|
|
{
|
|
|
fileList.Add(fileName);
|
|
|
}
|
|
|
@@ -977,6 +987,7 @@ namespace UAS_MES_Tools
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ bool downloadSuccess = false;
|
|
|
string uri = $"{ftpServer.TrimEnd('/')}/{remoteFilePath.TrimStart('/')}";
|
|
|
|
|
|
var request = (FtpWebRequest)WebRequest.Create(uri);
|
|
|
@@ -997,7 +1008,22 @@ namespace UAS_MES_Tools
|
|
|
fileStream.Write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
|
|
|
+ downloadSuccess = true;
|
|
|
LogMessage($"OK,文件下载成功: {remoteFilePath} -> {localFilePath}");
|
|
|
+
|
|
|
+ if (downloadSuccess)
|
|
|
+ {
|
|
|
+ var deleteRequest = (FtpWebRequest)WebRequest.Create(uri);
|
|
|
+ deleteRequest.Method = WebRequestMethods.Ftp.DeleteFile;
|
|
|
+ deleteRequest.Credentials = new NetworkCredential(username, password);
|
|
|
+ deleteRequest.UsePassive = true;
|
|
|
+ deleteRequest.KeepAlive = false;
|
|
|
+
|
|
|
+ using (var deleteResponse = (FtpWebResponse)deleteRequest.GetResponse())
|
|
|
+ {
|
|
|
+ LogMessage($"OK,服务器文件删除成功: {remoteFilePath}");
|
|
|
+ }
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
@@ -1020,6 +1046,36 @@ namespace UAS_MES_Tools
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private string CreateFtpDirectoryIfNotExists(string ftpDirectoryPath, string username, string password)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpDirectoryPath);
|
|
|
+ request.Method = WebRequestMethods.Ftp.MakeDirectory;
|
|
|
+ request.Credentials = new NetworkCredential(username, password);
|
|
|
+ request.UsePassive = true;
|
|
|
+ request.UseBinary = true;
|
|
|
+ request.KeepAlive = false;
|
|
|
+
|
|
|
+ using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
|
|
|
+ {
|
|
|
+ response.Close();
|
|
|
+ return "OK,目录创建成功: " + response.StatusDescription;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (WebException ex)
|
|
|
+ {
|
|
|
+ if (ex.Response is FtpWebResponse response && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
|
|
|
+ {
|
|
|
+ return "OK,目录已存在: " + response.StatusDescription;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "NG,创建目录时发生错误: " + ex.Message;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void CompressFile(string sourceFile, string zipPath)
|
|
|
{
|
|
|
try
|
|
|
@@ -1067,6 +1123,7 @@ namespace UAS_MES_Tools
|
|
|
}
|
|
|
|
|
|
ZipFile.CreateFromDirectory(sourceFolder, zipPath);
|
|
|
+ Directory.Delete(sourceFolder, true);
|
|
|
LogMessage($"OK,文件已压缩到:{zipPath}");
|
|
|
}
|
|
|
catch (UnauthorizedAccessException ex)
|