Browse Source

迭代优化

yhluo 2 days ago
parent
commit
ed4e7ba054
4 changed files with 110 additions and 52 deletions
  1. 3 3
      UAS_Tools_HY/App.config
  2. 21 21
      UAS_Tools_HY/Main.Designer.cs
  3. 84 27
      UAS_Tools_HY/Main.cs
  4. 2 1
      UAS_Tools_HY/UAS_Tools_HY.csproj

+ 3 - 3
UAS_Tools_HY/App.config

@@ -1,6 +1,6 @@
-<?xml version="1.0" encoding="utf-8" ?>
+<?xml version="1.0" encoding="utf-8"?>
 <configuration>
 <configuration>
   <startup>
   <startup>
-    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
+    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
   </startup>
   </startup>
-</configuration>
+</configuration>

+ 21 - 21
UAS_Tools_HY/Main.Designer.cs

@@ -121,6 +121,8 @@
             this.Page2 = new System.Windows.Forms.TabPage();
             this.Page2 = new System.Windows.Forms.TabPage();
             this.MsgBox = new System.Windows.Forms.ListBox();
             this.MsgBox = new System.Windows.Forms.ListBox();
             this.FileDetail_Box = new System.Windows.Forms.Panel();
             this.FileDetail_Box = new System.Windows.Forms.Panel();
+            this.FileCountVal = new System.Windows.Forms.TextBox();
+            this.FileCountLab = new System.Windows.Forms.Label();
             this.timerLab1 = new System.Windows.Forms.Label();
             this.timerLab1 = new System.Windows.Forms.Label();
             this.FtpListen = new System.Windows.Forms.Button();
             this.FtpListen = new System.Windows.Forms.Button();
             this.FtpConnect = new System.Windows.Forms.Button();
             this.FtpConnect = new System.Windows.Forms.Button();
@@ -136,8 +138,6 @@
             this.currUser = new System.Windows.Forms.Label();
             this.currUser = new System.Windows.Forms.Label();
             this.currTime = new System.Windows.Forms.Label();
             this.currTime = new System.Windows.Forms.Label();
             this.top = new System.Windows.Forms.Panel();
             this.top = new System.Windows.Forms.Panel();
-            this.FileCountVal = new System.Windows.Forms.TextBox();
-            this.FileCountLab = new System.Windows.Forms.Label();
             this.Pagination.SuspendLayout();
             this.Pagination.SuspendLayout();
             this.Page1.SuspendLayout();
             this.Page1.SuspendLayout();
             this.checkPagination.SuspendLayout();
             this.checkPagination.SuspendLayout();
@@ -1215,6 +1215,25 @@
             this.FileDetail_Box.Size = new System.Drawing.Size(1240, 188);
             this.FileDetail_Box.Size = new System.Drawing.Size(1240, 188);
             this.FileDetail_Box.TabIndex = 1;
             this.FileDetail_Box.TabIndex = 1;
             // 
             // 
+            // FileCountVal
+            // 
+            this.FileCountVal.Anchor = System.Windows.Forms.AnchorStyles.Left;
+            this.FileCountVal.Location = new System.Drawing.Point(629, 19);
+            this.FileCountVal.Name = "FileCountVal";
+            this.FileCountVal.Size = new System.Drawing.Size(65, 35);
+            this.FileCountVal.TabIndex = 13;
+            this.FileCountVal.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
+            // 
+            // FileCountLab
+            // 
+            this.FileCountLab.Anchor = System.Windows.Forms.AnchorStyles.Left;
+            this.FileCountLab.AutoSize = true;
+            this.FileCountLab.Location = new System.Drawing.Point(515, 22);
+            this.FileCountLab.Name = "FileCountLab";
+            this.FileCountLab.Size = new System.Drawing.Size(122, 28);
+            this.FileCountLab.TabIndex = 12;
+            this.FileCountLab.Text = "打包文件数:";
+            // 
             // timerLab1
             // timerLab1
             // 
             // 
             this.timerLab1.Anchor = System.Windows.Forms.AnchorStyles.Left;
             this.timerLab1.Anchor = System.Windows.Forms.AnchorStyles.Left;
@@ -1375,25 +1394,6 @@
             this.top.Size = new System.Drawing.Size(1254, 625);
             this.top.Size = new System.Drawing.Size(1254, 625);
             this.top.TabIndex = 3;
             this.top.TabIndex = 3;
             // 
             // 
-            // FileCountVal
-            // 
-            this.FileCountVal.Anchor = System.Windows.Forms.AnchorStyles.Left;
-            this.FileCountVal.Location = new System.Drawing.Point(629, 19);
-            this.FileCountVal.Name = "FileCountVal";
-            this.FileCountVal.Size = new System.Drawing.Size(65, 35);
-            this.FileCountVal.TabIndex = 13;
-            this.FileCountVal.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
-            // 
-            // FileCountLab
-            // 
-            this.FileCountLab.Anchor = System.Windows.Forms.AnchorStyles.Left;
-            this.FileCountLab.AutoSize = true;
-            this.FileCountLab.Location = new System.Drawing.Point(515, 22);
-            this.FileCountLab.Name = "FileCountLab";
-            this.FileCountLab.Size = new System.Drawing.Size(122, 28);
-            this.FileCountLab.TabIndex = 12;
-            this.FileCountLab.Text = "打包文件数:";
-            // 
             // Main
             // Main
             // 
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
             this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);

+ 84 - 27
UAS_Tools_HY/Main.cs

@@ -19,6 +19,7 @@ using System.Windows.Forms;
 using UAS_Tools_HY;
 using UAS_Tools_HY;
 using UAS_Tools_HY.Properties;
 using UAS_Tools_HY.Properties;
 using UAS_Tools_HY.PublicMethods;
 using UAS_Tools_HY.PublicMethods;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 using Application = Microsoft.Office.Interop.Excel.Application;
 using Application = Microsoft.Office.Interop.Excel.Application;
 using DataTable = System.Data.DataTable;
 using DataTable = System.Data.DataTable;
 
 
@@ -41,6 +42,7 @@ namespace UAS_MES_Tools
 
 
         string saveFiles;
         string saveFiles;
 
 
+        int currentNum = 1;
         public Main(string account, string password)
         public Main(string account, string password)
         {
         {
             _Account = account;
             _Account = account;
@@ -87,11 +89,11 @@ namespace UAS_MES_Tools
                 Directory.CreateDirectory(saveFiles);
                 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 = new Timer();
             File_timer.Interval = 30 * 1000;
             File_timer.Interval = 30 * 1000;
@@ -99,7 +101,7 @@ namespace UAS_MES_Tools
 
 
             timerVal.Text = "30";
             timerVal.Text = "30";
             FileCountVal.Text = "10";
             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)
         private void SN_KeyDown(object sender, KeyEventArgs e)
@@ -747,6 +749,7 @@ namespace UAS_MES_Tools
                     FtpPwVal.Enabled = false;
                     FtpPwVal.Enabled = false;
                     timerVal.Enabled = false;
                     timerVal.Enabled = false;
                     FtpPathVal.Enabled = false;
                     FtpPathVal.Enabled = false;
+                    FileCountVal.Enabled = false;
                     FtpConnect.Text = "关闭连接";
                     FtpConnect.Text = "关闭连接";
                     FtpListen.Enabled = true;
                     FtpListen.Enabled = true;
                 }
                 }
@@ -762,6 +765,7 @@ namespace UAS_MES_Tools
                 FtpPwVal.Enabled = true;
                 FtpPwVal.Enabled = true;
                 timerVal.Enabled = true;
                 timerVal.Enabled = true;
                 FtpPathVal.Enabled = true;
                 FtpPathVal.Enabled = true;
+                FileCountVal.Enabled = true;
                 FtpConnect.Text = "开启连接";
                 FtpConnect.Text = "开启连接";
                 FtpListen.Enabled = false;
                 FtpListen.Enabled = false;
             }
             }
@@ -866,10 +870,12 @@ namespace UAS_MES_Tools
         {
         {
             try
             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();
                 string ftpServer = FtpPathVal.Text.Trim();
@@ -892,8 +898,13 @@ namespace UAS_MES_Tools
 
 
                 foreach (string remoteFile in fileList)
                 foreach (string remoteFile in fileList)
                 {
                 {
+                    if (!Directory.Exists(childSaveFiles))
+                    {
+                        Directory.CreateDirectory(childSaveFiles);
+                    }
+
                     string remoteFilePath = $"{remoteFolderPath.TrimEnd('/')}/{remoteFile}";
                     string remoteFilePath = $"{remoteFolderPath.TrimEnd('/')}/{remoteFile}";
-                    string localFilePath = Path.Combine(saveFiles, remoteFile);
+                    string localFilePath = Path.Combine(childSaveFiles, remoteFile);
 
 
                     if (DownloadSingleFile(ftpServer, remoteFilePath, localFilePath, username, password))
                     if (DownloadSingleFile(ftpServer, remoteFilePath, localFilePath, username, password))
                     {
                     {
@@ -903,19 +914,17 @@ namespace UAS_MES_Tools
                     {
                     {
                         failCount++;
                         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)
             catch (Exception ex)
             {
             {
@@ -946,17 +955,18 @@ namespace UAS_MES_Tools
                     string line;
                     string line;
                     while ((line = reader.ReadLine()) != null)
                     while ((line = reader.ReadLine()) != null)
                     {
                     {
-                        // 解析FTP返回的行,提取文件名
-                        // FTP返回格式通常类似于: "-rw-r--r-- 1 ftp ftp 12345 Jan 1 10:00 filename.txt"
                         if (!string.IsNullOrWhiteSpace(line))
                         if (!string.IsNullOrWhiteSpace(line))
                         {
                         {
                             string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                             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);
                                     fileList.Add(fileName);
                                 }
                                 }
@@ -977,6 +987,7 @@ namespace UAS_MES_Tools
         {
         {
             try
             try
             {
             {
+                bool downloadSuccess = false;
                 string uri = $"{ftpServer.TrimEnd('/')}/{remoteFilePath.TrimStart('/')}";
                 string uri = $"{ftpServer.TrimEnd('/')}/{remoteFilePath.TrimStart('/')}";
 
 
                 var request = (FtpWebRequest)WebRequest.Create(uri);
                 var request = (FtpWebRequest)WebRequest.Create(uri);
@@ -997,7 +1008,22 @@ namespace UAS_MES_Tools
                         fileStream.Write(buffer, 0, bytesRead);
                         fileStream.Write(buffer, 0, bytesRead);
                     }
                     }
 
 
+                    downloadSuccess = true;
                     LogMessage($"OK,文件下载成功: {remoteFilePath} -> {localFilePath}");
                     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;
                     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)
         private void CompressFile(string sourceFile, string zipPath)
         {
         {
             try
             try
@@ -1067,6 +1123,7 @@ namespace UAS_MES_Tools
                 }
                 }
 
 
                 ZipFile.CreateFromDirectory(sourceFolder, zipPath);
                 ZipFile.CreateFromDirectory(sourceFolder, zipPath);
+                Directory.Delete(sourceFolder, true);
                 LogMessage($"OK,文件已压缩到:{zipPath}");
                 LogMessage($"OK,文件已压缩到:{zipPath}");
             }
             }
             catch (UnauthorizedAccessException ex)
             catch (UnauthorizedAccessException ex)

+ 2 - 1
UAS_Tools_HY/UAS_Tools_HY.csproj

@@ -8,7 +8,7 @@
     <OutputType>WinExe</OutputType>
     <OutputType>WinExe</OutputType>
     <RootNamespace>UAS_Tools_HY</RootNamespace>
     <RootNamespace>UAS_Tools_HY</RootNamespace>
     <AssemblyName>UAS_Tools_HY</AssemblyName>
     <AssemblyName>UAS_Tools_HY</AssemblyName>
-    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <Deterministic>true</Deterministic>
     <Deterministic>true</Deterministic>
@@ -27,6 +27,7 @@
     <IsWebBootstrapper>false</IsWebBootstrapper>
     <IsWebBootstrapper>false</IsWebBootstrapper>
     <UseApplicationTrust>false</UseApplicationTrust>
     <UseApplicationTrust>false</UseApplicationTrust>
     <BootstrapperEnabled>true</BootstrapperEnabled>
     <BootstrapperEnabled>true</BootstrapperEnabled>
+    <TargetFrameworkProfile />
   </PropertyGroup>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
     <PlatformTarget>AnyCPU</PlatformTarget>