Browse Source

ERP打印插件调整提交

caosy 5 years ago
parent
commit
94d16e9f86

+ 15 - 3
UAS_PRINT/Form2.Designer.cs

@@ -34,6 +34,7 @@
             this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
             this.打开根目录ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
+            this.FTPtoolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
             this.contextMenuStrip1.SuspendLayout();
             this.SuspendLayout();
             // 
@@ -48,24 +49,34 @@
             // 
             this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
             this.打开根目录ToolStripMenuItem,
+            this.FTPtoolStripMenuItem,
             this.退出ToolStripMenuItem});
             this.contextMenuStrip1.Name = "contextMenuStrip1";
-            this.contextMenuStrip1.Size = new System.Drawing.Size(137, 48);
+            this.contextMenuStrip1.ShowCheckMargin = true;
+            this.contextMenuStrip1.Size = new System.Drawing.Size(175, 92);
             // 
             // 打开根目录ToolStripMenuItem
             // 
             this.打开根目录ToolStripMenuItem.Name = "打开根目录ToolStripMenuItem";
-            this.打开根目录ToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
+            this.打开根目录ToolStripMenuItem.Size = new System.Drawing.Size(174, 22);
             this.打开根目录ToolStripMenuItem.Text = "打开根目录";
             this.打开根目录ToolStripMenuItem.Click += new System.EventHandler(this.打开根目录ToolStripMenuItem_Click);
             // 
             // 退出ToolStripMenuItem
             // 
             this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
-            this.退出ToolStripMenuItem.Size = new System.Drawing.Size(136, 22);
+            this.退出ToolStripMenuItem.Size = new System.Drawing.Size(174, 22);
             this.退出ToolStripMenuItem.Text = "退出";
             this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
             // 
+            // FTPtoolStripMenuItem
+            // 
+            this.FTPtoolStripMenuItem.CheckOnClick = true;
+            this.FTPtoolStripMenuItem.Name = "FTPtoolStripMenuItem";
+            this.FTPtoolStripMenuItem.Size = new System.Drawing.Size(174, 22);
+            this.FTPtoolStripMenuItem.Text = "是否启动FTP";
+            this.FTPtoolStripMenuItem.Click += new System.EventHandler(this.FTPtoolStripMenuItem_Click);
+            // 
             // Form2
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
@@ -89,5 +100,6 @@
         private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
         private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
         private System.Windows.Forms.ToolStripMenuItem 打开根目录ToolStripMenuItem;
+        private System.Windows.Forms.ToolStripMenuItem FTPtoolStripMenuItem;
     }
 }

+ 9 - 1
UAS_PRINT/Form2.cs

@@ -12,6 +12,7 @@ using System.Net;
 using System.Text;
 using System.Threading;
 using System.Windows.Forms;
+using UAS_PRINT.Properties;
 
 namespace UAS_PRINT
 {
@@ -21,7 +22,6 @@ namespace UAS_PRINT
         {
             InitializeComponent();
         }
-
         private static HttpListener httpPostRequest = new HttpListener();
         private Thread ThrednHttpPostRequest;
         private void Form2_Load(object sender, EventArgs e)
@@ -39,6 +39,8 @@ namespace UAS_PRINT
             ThrednHttpPostRequest.Start();
             提示.ShowBalloonTip(30, "提示", "程序启动", ToolTipIcon.Info);
 
+            FTPtoolStripMenuItem.Checked = Settings.Default.ifuseftp;
+
             //string req = File.ReadAllText(@"C:\Users\Hcsy\Documents\Tencent Files\814802334\FileRecv\4.txt", Encoding.UTF8);
             //if (req.Contains("\"" + "chooseprintername" + "\""))
             //{
@@ -182,5 +184,11 @@ namespace UAS_PRINT
         {
             Visible = false;
         }
+
+        private void FTPtoolStripMenuItem_Click(object sender, EventArgs e)
+        {
+            Settings.Default.ifuseftp = FTPtoolStripMenuItem.Checked;
+            Settings.Default.Save();
+        }
     }
 }

+ 14 - 0
UAS_PRINT/PrintHandler.cs

@@ -8,6 +8,7 @@ using System.Text;
 using System.Text.RegularExpressions;
 using System.Windows.Forms;
 using System.Drawing.Printing;
+using UAS_PRINT.Properties;
 
 namespace UAS_PRINT
 {
@@ -18,6 +19,7 @@ namespace UAS_PRINT
         static List<Bitmap> bitmaps = new List<Bitmap>();
         static DataTable dt = new DataTable();
      
+
         public static void zplprint(string str, string PrinterName)
         {
             Dictionary<string, object> data = PrintHelper.ToDictionary(str);
@@ -116,6 +118,18 @@ namespace UAS_PRINT
                 MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
                 return;
             }
+            if (Settings.Default.ifuseftp)
+            {
+                ftpOperater frp = new ftpOperater(true);
+                try
+                {
+                    frp.Download(label_name + ".frx", DateTime.Now);
+                }
+                catch (Exception ex)
+                {
+                    MessageBox.Show("标签文件更新失败" + ex.Message);
+                }
+            }
             try
             {
                 Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");

+ 61 - 17
UAS_PRINT/Properties/Settings.Designer.cs

@@ -1,30 +1,74 @@
 //------------------------------------------------------------------------------
 // <auto-generated>
-//     This code was generated by a tool.
-//     Runtime Version:4.0.30319.42000
+//     此代码由工具生成。
+//     运行时版本:4.0.30319.42000
 //
-//     Changes to this file may cause incorrect behavior and will be lost if
-//     the code is regenerated.
+//     对此文件的更改可能会导致不正确的行为,并且如果
+//     重新生成代码,这些更改将会丢失。
 // </auto-generated>
 //------------------------------------------------------------------------------
 
-namespace UAS_PRINT.Properties
-{
-
-
+namespace UAS_PRINT.Properties {
+    
+    
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
-    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
-    {
-
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")]
+    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
+        
         private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
-
-        public static Settings Default
-        {
-            get
-            {
+        
+        public static Settings Default {
+            get {
                 return defaultInstance;
             }
         }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("ftp://172.16.0.20")]
+        public string ftpaddress {
+            get {
+                return ((string)(this["ftpaddress"]));
+            }
+            set {
+                this["ftpaddress"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("vsftpd")]
+        public string ftpuser {
+            get {
+                return ((string)(this["ftpuser"]));
+            }
+            set {
+                this["ftpuser"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("shenaftp")]
+        public string ftppassword {
+            get {
+                return ((string)(this["ftppassword"]));
+            }
+            set {
+                this["ftppassword"] = value;
+            }
+        }
+        
+        [global::System.Configuration.UserScopedSettingAttribute()]
+        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+        [global::System.Configuration.DefaultSettingValueAttribute("False")]
+        public bool ifuseftp {
+            get {
+                return ((bool)(this["ifuseftp"]));
+            }
+            set {
+                this["ifuseftp"] = value;
+            }
+        }
     }
 }

+ 17 - 6
UAS_PRINT/Properties/Settings.settings

@@ -1,7 +1,18 @@
 <?xml version='1.0' encoding='utf-8'?>
-<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
-  <Profiles>
-    <Profile Name="(Default)" />
-  </Profiles>
-  <Settings />
-</SettingsFile>
+<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="UAS_PRINT.Properties" GeneratedClassName="Settings">
+  <Profiles />
+  <Settings>
+    <Setting Name="ftpaddress" Type="System.String" Scope="User">
+      <Value Profile="(Default)">ftp://172.16.0.20</Value>
+    </Setting>
+    <Setting Name="ftpuser" Type="System.String" Scope="User">
+      <Value Profile="(Default)">vsftpd</Value>
+    </Setting>
+    <Setting Name="ftppassword" Type="System.String" Scope="User">
+      <Value Profile="(Default)">shenaftp</Value>
+    </Setting>
+    <Setting Name="ifuseftp" Type="System.Boolean" Scope="User">
+      <Value Profile="(Default)">False</Value>
+    </Setting>
+  </Settings>
+</SettingsFile>

+ 28 - 0
UAS_PRINT/Settings.cs

@@ -0,0 +1,28 @@
+namespace UAS_PRINT.Properties {
+    
+    
+    // 通过此类可以处理设置类的特定事件: 
+    //  在更改某个设置的值之前将引发 SettingChanging 事件。
+    //  在更改某个设置的值之后将引发 PropertyChanged 事件。
+    //  在加载设置值之后将引发 SettingsLoaded 事件。
+    //  在保存设置值之前将引发 SettingsSaving 事件。
+    internal sealed partial class Settings {
+        
+        public Settings() {
+            // // 若要为保存和更改设置添加事件处理程序,请取消注释下列行: 
+            //
+            // this.SettingChanging += this.SettingChangingEventHandler;
+            //
+            // this.SettingsSaving += this.SettingsSavingEventHandler;
+            //
+        }
+        
+        private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
+            // 在此处添加用于处理 SettingChangingEvent 事件的代码。
+        }
+        
+        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {
+            // 在此处添加用于处理 SettingsSaving 事件的代码。
+        }
+    }
+}

+ 3 - 0
UAS_PRINT/UAS_PRINT.csproj

@@ -69,6 +69,7 @@
     <Compile Include="Form2.Designer.cs">
       <DependentUpon>Form2.cs</DependentUpon>
     </Compile>
+    <Compile Include="ftpOperater.cs" />
     <Compile Include="PrinterList.cs">
       <SubType>Form</SubType>
     </Compile>
@@ -79,6 +80,7 @@
     <Compile Include="PrintHelper.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Settings.cs" />
     <EmbeddedResource Include="Form2.resx">
       <DependentUpon>Form2.cs</DependentUpon>
     </EmbeddedResource>
@@ -94,6 +96,7 @@
       <AutoGen>True</AutoGen>
       <DependentUpon>Resources.resx</DependentUpon>
     </Compile>
+    <None Include="app.config" />
     <None Include="packages.config" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>

+ 24 - 0
UAS_PRINT/app.config

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <configSections>
+        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
+            <section name="UAS_PRINT.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
+        </sectionGroup>
+    </configSections>
+    <userSettings>
+        <UAS_PRINT.Properties.Settings>
+            <setting name="ftpaddress" serializeAs="String">
+                <value>ftp://172.16.0.20</value>
+            </setting>
+            <setting name="ftpuser" serializeAs="String">
+                <value>vsftpd</value>
+            </setting>
+            <setting name="ftppassword" serializeAs="String">
+                <value>shenaftp</value>
+            </setting>
+            <setting name="ifuseftp" serializeAs="String">
+                <value>False</value>
+            </setting>
+        </UAS_PRINT.Properties.Settings>
+    </userSettings>
+</configuration>

+ 190 - 0
UAS_PRINT/ftpOperater.cs

@@ -0,0 +1,190 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Net;
+using System.Text;
+using System.Windows.Forms;
+using UAS_PRINT.Properties;
+
+namespace UAS_PRINT
+{
+    class ftpOperater
+    {
+       // public static string FTPAddress = "ftp://172.16.0.20|vsftpd|shenaftp";
+
+        public static bool Inner = false;
+
+        public static string DownLoadTo = AppDomain.CurrentDomain.BaseDirectory;
+
+        private string ftpServerIP;
+        private string ftpUser;
+        private string ftpPwd;
+
+        public ftpOperater(bool PortModel)
+        {
+            //if (!Inner)
+            //{
+            //    FTPInf = FTPAddress.Split('|');
+            //}
+            //else
+            //{
+            //    FTPInf = FTPAddress.Split('|');
+            //}
+
+            this.ftpServerIP = Settings.Default.ftpaddress;
+            this.ftpUser = Settings.Default.ftpuser;
+            this.ftpPwd = Settings.Default.ftppassword;
+        } 
+
+        public void UpLoadFile(string filepath, string filename)
+        {
+            //上传之前判断文件是否存在
+            string[] filelist = GetFileList();
+            if (filelist != null)
+                for (int i = 0; i < filelist.Length; i++)
+                {
+                    if (filelist[i] == filename)
+                    {
+                        //string upload = MessageBox.Show("已存在同名文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
+                        //if (upload.ToString() != "Yes")
+                        //{
+                        //    return;
+                        //}
+                    }
+                }
+            FtpWebRequest reqFTP;
+            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
+            reqFTP.UseBinary = true;
+            reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
+            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
+            FileInfo file = new FileInfo(filepath + "/" + filename);
+            const int BufferSize = 2048;
+            byte[] content = new byte[BufferSize - 1 + 1];
+            int dataRead;
+            using (FileStream fs = file.OpenRead())
+            {
+                //把上传的文件写入流
+                using (Stream rs = reqFTP.GetRequestStream())
+                {
+                    do
+                    {
+                        //每次读文件流的2KB
+                        dataRead = fs.Read(content, 0, BufferSize);
+                        rs.Write(content, 0, dataRead);
+                    } while (!(dataRead < BufferSize));
+                    rs.Close();
+                }
+                fs.Close();
+            }
+        }
+
+        /// <summary>
+        /// 获取ftp服务器上的文件信息
+        /// </summary>
+        /// <returns>存储了所有文件信息的字符串数组</returns>
+        public string[] GetFileList()
+        {
+            string[] downloadFiles;
+            StringBuilder result = new StringBuilder();
+            FtpWebRequest reqFTP;
+            try
+            {
+                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/"));
+                reqFTP.UseBinary = true;
+                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
+                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
+                WebResponse response = reqFTP.GetResponse();
+                StreamReader reader = new StreamReader(response.GetResponseStream());
+
+                string line = reader.ReadLine();
+                while (line != null)
+                {
+                    result.Append(line);
+                    result.Append("\n");
+                    line = reader.ReadLine();
+                }
+                result.Remove(result.ToString().LastIndexOf('\n'), 1);
+                reader.Close();
+                response.Close();
+
+                return result.ToString().Split('\n');
+            }
+            catch (Exception ex)
+            {
+                System.Windows.Forms.MessageBox.Show("获取文件信息失败:" + ex.Message, "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                downloadFiles = null;
+                return downloadFiles;
+            }
+        }
+
+        /// <summary>
+        /// 获取FTP上指定文件的大小
+        /// </summary>
+        /// <param name="filename">文件名</param>
+        /// <returns>文件大小</returns>
+        public long GetFileSize(string filename)
+        {
+            FtpWebRequest reqFTP;
+            long fileSize = 0;
+            try
+            {
+                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
+                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
+                reqFTP.UseBinary = true;
+                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
+                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
+                Stream ftpStream = response.GetResponseStream();
+                fileSize = response.ContentLength;
+                ftpStream.Close();
+                response.Close();
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show("获取文件大小时,出现异常:\n" + ex.Message, "获取文件大小失败!", MessageBoxButtons.OK, MessageBoxIcon.Error);
+            }
+            return fileSize;
+        }
+
+        /// <summary>
+        /// 实现ftp下载操作
+        /// </summary>
+        /// <param name="fileName">远程文件名</param>
+        public string Download(string fileName, DateTime time)
+        {
+            FtpWebRequest reqFTP;
+            try
+            {
+                FileStream outputStream = new FileStream(DownLoadTo + @"\" + fileName, FileMode.Create);
+                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
+                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
+                reqFTP.UseBinary = true;
+                reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
+                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
+                Stream ftpStream = response.GetResponseStream();
+                long cl = response.ContentLength;
+                int bufferSize = 4096;
+                int readCount;
+                byte[] buffer = new byte[bufferSize];
+                readCount = ftpStream.Read(buffer, 0, bufferSize);
+                while (readCount > 0)
+                {
+                    outputStream.Write(buffer, 0, readCount);
+                    readCount = ftpStream.Read(buffer, 0, bufferSize);
+                }
+                ftpStream.Close();
+                outputStream.Close();
+                response.Close();
+                //设置最后修改文件时间为服务器时间
+                FileInfo f = new FileInfo(DownLoadTo + @"\" + fileName);
+                f.LastWriteTime = time;
+                return DownLoadTo + @"\" + fileName;
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.Message);
+                return "";
+            }
+        }
+    }
+}