Эх сурвалжийг харах

增加共享文件夹连接方法

Hcsy 8 жил өмнө
parent
commit
7c7b547afb

+ 1 - 1
UAS-MES/FunctionCode/SystemSetting/SystemSetting_LabelMaintain.Designer.cs

@@ -261,7 +261,7 @@
             this.SavePath.Name = "SavePath";
             this.SavePath.Size = new System.Drawing.Size(117, 21);
             this.SavePath.TabIndex = 210;
-            this.SavePath.Text = "\\\\172.16.11.99\\ShareTest\\";
+            this.SavePath.Text = "\\\\172.16.11.99\\ShareTest";
             // 
             // SavePath_label
             // 

+ 8 - 4
UAS-MES/FunctionCode/SystemSetting/SystemSetting_LabelMaintain.cs

@@ -57,6 +57,8 @@ namespace UAS_MES.SystemSetting
         //添加在grid末列的操作列
         DataTable dbfind;
 
+        ftpOperater ftp;
+
         Thread InitPrint;
         DataGridViewImageColumn PerviewColumn = new DataGridViewImageColumn
         {
@@ -121,6 +123,9 @@ namespace UAS_MES.SystemSetting
             pr_spec.KeyDown += ScreenEvent;
 
             //SavePath.Text = BaseUtil.GetCacheData("SavePath").ToString();
+            BaseUtil.SetCacheData("SavePath", SavePath.Text);
+
+            ftp = new ftpOperater();
         }
         private void InPrint()
         {
@@ -450,7 +455,7 @@ namespace UAS_MES.SystemSetting
                 if (FTPShare.Checked)
                 {
                     string SoftWare = "CodeSoft";
-                    ftpOperater ftp = new ftpOperater();
+                  
                     ftp.UpLoadFile(FolderPath.Text, la_name.Text,SavePath.Text);
                     if (pl_id2 != "")
                     {
@@ -459,7 +464,7 @@ namespace UAS_MES.SystemSetting
                             sql.Clear();
                             sql.Append("update productlabel set pl_labelcode=:pl_labelcode,pl_labeltype=:pl_labeltype,pl_labelurl=:pl_labelurl,pl_labelname=:pl_labelname,");
                             sql.Append("pl_prodcode=:pl_prodcode,pl_indate=sysdate where pl_id='" + pl_id2 + "'");
-                            dh.ExecuteSql(sql.GetString(), "update", la_code.Text, la_type.Text,SavePath.Text + la_name.Text,la_name.Text, pr_code.Text);
+                            dh.ExecuteSql(sql.GetString(), "update", la_code.Text, la_type.Text,SavePath.Text + @"\" + la_name.Text,la_name.Text, pr_code.Text);
                         }
                         else
                         {
@@ -475,7 +480,7 @@ namespace UAS_MES.SystemSetting
                         {
                             sql.Clear();
                             sql.Append("insert into productlabel (pl_id,pl_labelcode,pl_labeltype,pl_isdefault,pl_labelurl,pl_labelsoft,pl_labelname,pl_prodcode,pl_indate) values( productlabel_seq.nextval, ");
-                            sql.Append("'" + la_code.Text + "','" + la_type.Text + "','" + isdefault(pr_code.Text, la_type.Text) + "','" + SavePath.Text + la_name.Text + "','" + SoftWare + "','" + la_name.Text + "','" + pr_code.Text + "',sysdate)");
+                            sql.Append("'" + la_code.Text + "','" + la_type.Text + "','" + isdefault(pr_code.Text, la_type.Text) + "','" + SavePath.Text + @"\" + la_name.Text + "','" + SoftWare + "','" + la_name.Text + "','" + pr_code.Text + "',sysdate)");
                             dh.ExecuteSql(sql.GetString(), "insert");
                         }
                         else
@@ -531,7 +536,6 @@ namespace UAS_MES.SystemSetting
                 }
             }
             dh.DeleteDataByID("productlabel", "pl_id", deleteid);
-            ftpOperater ftp = new ftpOperater();
             for (int i = 0; i < deletelabname.Length; i++)
             {
                 if (deletelabname[i] != null)

+ 97 - 3
UAS-MES/PublicMethod/BaseUtil.cs

@@ -237,8 +237,8 @@ namespace UAS_MES.PublicMethod
         /// <returns></returns>
         public static string GetLabelUrl(string URL, string LabelName, System.DateTime time)
         {
-             ftpOperater ftp = new ftpOperater();
-             return ftp.DownLoadFromSharePath(URL, LabelName);        
+            ftpOperater ftp = new ftpOperater();
+            return ftp.DownLoadFromSharePath(URL, LabelName);
         }
 
 
@@ -1104,5 +1104,99 @@ namespace UAS_MES.PublicMethod
 
             }
         }
+
+
+        public static bool connectState(string path)
+        {
+            return connectState(path, "", "");
+        }
+        /// <summary>
+        /// 连接远程共享文件夹
+        /// </summary>
+        /// <param name="path">远程共享文件夹的路径</param>
+        /// <param name="userName">用户名</param>
+        /// <param name="passWord">密码</param>
+        /// <returns></returns>
+        public static bool connectState(string path, string userName, string passWord)
+        {
+            bool Flag = false;
+            Process proc = new Process();
+            try
+            {
+                proc.StartInfo.FileName = "cmd.exe";
+                proc.StartInfo.UseShellExecute = false;
+                proc.StartInfo.RedirectStandardInput = true;
+                proc.StartInfo.RedirectStandardOutput = true;
+                proc.StartInfo.RedirectStandardError = true;
+                proc.StartInfo.CreateNoWindow = true;
+                proc.Start();
+                string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
+                proc.StandardInput.WriteLine(dosLine);
+                proc.StandardInput.WriteLine("exit");
+                while (!proc.HasExited)
+                {
+                    proc.WaitForExit(1000);
+                }
+                string errormsg = proc.StandardError.ReadToEnd();
+                proc.StandardError.Close();
+                if (string.IsNullOrEmpty(errormsg))
+                {
+                    Flag = true;
+                }
+                else
+                {
+                    throw new Exception(errormsg);
+                }
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+            finally
+            {
+                proc.Close();
+                proc.Dispose();
+            }
+            return Flag;
+        }
+
+        /// <summary>
+        /// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
+        /// </summary>
+        /// <param name="src">要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"</param>
+        /// <param name="dst">保存文件的路径,不含名称及扩展名</param>
+        /// <param name="fileName">保存文件的名称以及扩展名</param>
+        public static void Transport(string src, string dst, string fileName)
+        {
+
+            FileStream inFileStream = new FileStream(src, FileMode.Open);
+            if (!Directory.Exists(dst))
+            {
+                Directory.CreateDirectory(dst);
+            }
+            dst = dst + fileName;
+            FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
+
+            byte[] buf = new byte[inFileStream.Length];
+
+            int byteCount;
+
+            while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
+            {
+
+                outFileStream.Write(buf, 0, byteCount);
+
+            }
+
+            inFileStream.Flush();
+
+            inFileStream.Close();
+
+            outFileStream.Flush();
+
+            outFileStream.Close();
+
+        }
     }
-}
+}
+

+ 42 - 21
UAS-MES/PublicMethod/ftpOperater.cs

@@ -17,6 +17,7 @@ namespace UAS_MES.PublicMethod
         private string ftpServerIP;
         private string ftpUser;
         private string ftpPwd;
+        bool status = false;
 
         public ftpOperater()
         {
@@ -24,8 +25,13 @@ namespace UAS_MES.PublicMethod
             this.ftpServerIP = FTPInf[0];
             this.ftpUser = FTPInf[1];
             this.ftpPwd = FTPInf[2];
-        }
+          
+
+            //连接共享文件夹
+            status = BaseUtil.connectState(BaseUtil.GetCacheData("SavePath").ToString(), "Administrator", "malata@123456");
 
+        }
+        #region
         public void UpLoadFile(string filepath, string filename)
         {
             //上传之前判断文件是否存在
@@ -67,36 +73,51 @@ namespace UAS_MES.PublicMethod
                 fs.Close();
             }
         }
-
+        #endregion
 
 
         public void UpLoadFile(string filepath, string filename,string savepath)
         {
-
-            //目标路径
-            string targetPath = savepath;
-            //var file = Directory.GetFiles(targetPath);
-            string sourceFile = Path.Combine(filepath+@"\", filename);
-            string destFile = Path.Combine(targetPath, filename);
-            //获取指定路径下的全部文件名
-            var file = Directory.GetFiles(targetPath);
-            string overwrite = "";
-            for (int i = 0; i < file.Length; i++)
+            if (status)
             {
-                if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == filename)
+                //目标路径
+                string targetPath = savepath;
+                //var file = Directory.GetFiles(targetPath);
+                string sourceFile = Path.Combine(filepath + @"\", filename);
+                string destFile = Path.Combine(targetPath + @"\", filename);
+                //获取指定路径下的全部文件名
+                var file = Directory.GetFiles(targetPath);
+                string overwrite = "";
+                for (int i = 0; i < file.Length; i++)
+                {
+                    if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == filename)
+                    {
+                        overwrite = MessageBox.Show("已存在名为" + filename + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
+                        break;
+                    }
+                }
+                if (overwrite == "Yes" || overwrite == "")
                 {
-                    overwrite = MessageBox.Show("已存在名为" + filename + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
-                    break;
+                    //不存在文件的话进行创建
+                    if (!Directory.Exists(targetPath))
+                        Directory.CreateDirectory(targetPath);
+                    //将文件复制到指定位置
+                    File.Copy(sourceFile, destFile, true);
                 }
+                //string sourceFile = Path.Combine(filepath+@"\", filename);
+                ////共享文件夹的目录
+                //DirectoryInfo theFolder = new DirectoryInfo(savepath);
+                ////获取保存文件的路径
+                //string fielpath = theFolder.ToString() + @"\";
+                ////执行方法
+                //BaseUtil.Transport(sourceFile, fielpath, filename);
             }
-            if (overwrite == "Yes" || overwrite == "")
+            else
             {
-                //不存在文件的话进行创建
-                if (!Directory.Exists(targetPath))
-                    Directory.CreateDirectory(targetPath);
-                //将文件复制到指定位置
-                File.Copy(sourceFile, destFile, true);
+                //ListBox1.Items.Add("未能连接!");
+                MessageBox.Show("共享文件连接错误");
             }
+         
         }
 
         /// <summary>