Explorar o código

修改东创伟日志上传内容

callm %!s(int64=3) %!d(string=hai) anos
pai
achega
2b3a34d9b2

+ 18 - 86
FileWatcher/AutoAnalysisXml.Designer.cs

@@ -38,9 +38,6 @@
             this.ChooseFolder = new System.Windows.Forms.Button();
             this.OperateResult = new System.Windows.Forms.RichTextBox();
             this.Clean = new System.Windows.Forms.Button();
-            this.ChooseBackUpFolder = new System.Windows.Forms.Button();
-            this.BackUpFolderPath = new System.Windows.Forms.TextBox();
-            this.label2 = new System.Windows.Forms.Label();
             this.MakeCode_lable = new System.Windows.Forms.Label();
             this.label4 = new System.Windows.Forms.Label();
             this.Master = new System.Windows.Forms.ComboBox();
@@ -50,8 +47,6 @@
             this.Timer = new System.Windows.Forms.Timer(this.components);
             this.Device = new System.Windows.Forms.ComboBox();
             this.label3 = new System.Windows.Forms.Label();
-            this.button1 = new System.Windows.Forms.Button();
-            this.button2 = new System.Windows.Forms.Button();
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).BeginInit();
             this.SuspendLayout();
             // 
@@ -64,7 +59,7 @@
             // StartWatch
             // 
             this.StartWatch.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.StartWatch.Location = new System.Drawing.Point(290, 512);
+            this.StartWatch.Location = new System.Drawing.Point(290, 557);
             this.StartWatch.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.StartWatch.Name = "StartWatch";
             this.StartWatch.Size = new System.Drawing.Size(128, 56);
@@ -77,7 +72,7 @@
             // 
             this.StopWatch.Enabled = false;
             this.StopWatch.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.StopWatch.Location = new System.Drawing.Point(498, 512);
+            this.StopWatch.Location = new System.Drawing.Point(498, 557);
             this.StopWatch.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.StopWatch.Name = "StopWatch";
             this.StopWatch.Size = new System.Drawing.Size(128, 56);
@@ -90,7 +85,7 @@
             // 
             this.label1.AutoSize = true;
             this.label1.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label1.Location = new System.Drawing.Point(54, 316);
+            this.label1.Location = new System.Drawing.Point(48, 400);
             this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label1.Name = "label1";
             this.label1.Size = new System.Drawing.Size(162, 38);
@@ -100,7 +95,7 @@
             // FolderPath
             // 
             this.FolderPath.Enabled = false;
-            this.FolderPath.Location = new System.Drawing.Point(224, 316);
+            this.FolderPath.Location = new System.Drawing.Point(218, 400);
             this.FolderPath.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.FolderPath.Name = "FolderPath";
             this.FolderPath.Size = new System.Drawing.Size(436, 35);
@@ -109,7 +104,7 @@
             // ChooseFolder
             // 
             this.ChooseFolder.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.ChooseFolder.Location = new System.Drawing.Point(692, 316);
+            this.ChooseFolder.Location = new System.Drawing.Point(686, 394);
             this.ChooseFolder.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.ChooseFolder.Name = "ChooseFolder";
             this.ChooseFolder.Size = new System.Drawing.Size(156, 48);
@@ -123,14 +118,14 @@
             this.OperateResult.Location = new System.Drawing.Point(892, 22);
             this.OperateResult.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.OperateResult.Name = "OperateResult";
-            this.OperateResult.Size = new System.Drawing.Size(749, 535);
+            this.OperateResult.Size = new System.Drawing.Size(517, 535);
             this.OperateResult.TabIndex = 7;
             this.OperateResult.Text = "";
             // 
             // Clean
             // 
             this.Clean.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.Clean.Location = new System.Drawing.Point(1229, 579);
+            this.Clean.Location = new System.Drawing.Point(1096, 580);
             this.Clean.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.Clean.Name = "Clean";
             this.Clean.Size = new System.Drawing.Size(112, 48);
@@ -139,43 +134,11 @@
             this.Clean.UseVisualStyleBackColor = true;
             this.Clean.Click += new System.EventHandler(this.Clean_Click);
             // 
-            // ChooseBackUpFolder
-            // 
-            this.ChooseBackUpFolder.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.ChooseBackUpFolder.Location = new System.Drawing.Point(692, 402);
-            this.ChooseBackUpFolder.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
-            this.ChooseBackUpFolder.Name = "ChooseBackUpFolder";
-            this.ChooseBackUpFolder.Size = new System.Drawing.Size(156, 48);
-            this.ChooseBackUpFolder.TabIndex = 11;
-            this.ChooseBackUpFolder.Text = "选择文件夹";
-            this.ChooseBackUpFolder.UseVisualStyleBackColor = true;
-            this.ChooseBackUpFolder.Click += new System.EventHandler(this.ChooseBackUpFolder_Click);
-            // 
-            // BackUpFolderPath
-            // 
-            this.BackUpFolderPath.Enabled = false;
-            this.BackUpFolderPath.Location = new System.Drawing.Point(224, 402);
-            this.BackUpFolderPath.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
-            this.BackUpFolderPath.Name = "BackUpFolderPath";
-            this.BackUpFolderPath.Size = new System.Drawing.Size(436, 35);
-            this.BackUpFolderPath.TabIndex = 10;
-            // 
-            // label2
-            // 
-            this.label2.AutoSize = true;
-            this.label2.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label2.Location = new System.Drawing.Point(54, 402);
-            this.label2.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label2.Name = "label2";
-            this.label2.Size = new System.Drawing.Size(162, 38);
-            this.label2.TabIndex = 9;
-            this.label2.Text = "备份文件夹";
-            // 
             // MakeCode_lable
             // 
             this.MakeCode_lable.AutoSize = true;
             this.MakeCode_lable.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.MakeCode_lable.Location = new System.Drawing.Point(54, 227);
+            this.MakeCode_lable.Location = new System.Drawing.Point(48, 311);
             this.MakeCode_lable.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.MakeCode_lable.Name = "MakeCode_lable";
             this.MakeCode_lable.Size = new System.Drawing.Size(104, 38);
@@ -192,6 +155,7 @@
             this.label4.Size = new System.Drawing.Size(133, 38);
             this.label4.TabIndex = 14;
             this.label4.Text = "账套选择";
+            this.label4.Visible = false;
             // 
             // Master
             // 
@@ -202,6 +166,7 @@
             this.Master.Name = "Master";
             this.Master.Size = new System.Drawing.Size(436, 32);
             this.Master.TabIndex = 15;
+            this.Master.Visible = false;
             // 
             // AutoStart
             // 
@@ -213,11 +178,12 @@
             this.AutoStart.TabIndex = 16;
             this.AutoStart.Text = "开机自动启动";
             this.AutoStart.UseVisualStyleBackColor = true;
+            this.AutoStart.Visible = false;
             this.AutoStart.CheckedChanged += new System.EventHandler(this.AutoStart_CheckedChanged);
             // 
             // ma_code
             // 
-            this.ma_code.Location = new System.Drawing.Point(224, 227);
+            this.ma_code.Location = new System.Drawing.Point(218, 311);
             this.ma_code.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.ma_code.Name = "ma_code";
             this.ma_code.Size = new System.Drawing.Size(436, 35);
@@ -226,7 +192,7 @@
             // BatchAnalysus
             // 
             this.BatchAnalysus.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.BatchAnalysus.Location = new System.Drawing.Point(692, 224);
+            this.BatchAnalysus.Location = new System.Drawing.Point(686, 308);
             this.BatchAnalysus.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.BatchAnalysus.Name = "BatchAnalysus";
             this.BatchAnalysus.Size = new System.Drawing.Size(156, 48);
@@ -246,8 +212,8 @@
             this.Device.FormattingEnabled = true;
             this.Device.Items.AddRange(new object[] {
             "AOI设备",
-            "选焊设备"});
-            this.Device.Location = new System.Drawing.Point(224, 138);
+            "测试设备"});
+            this.Device.Location = new System.Drawing.Point(218, 222);
             this.Device.Margin = new System.Windows.Forms.Padding(6);
             this.Device.Name = "Device";
             this.Device.Size = new System.Drawing.Size(436, 32);
@@ -257,44 +223,18 @@
             // 
             this.label3.AutoSize = true;
             this.label3.Font = new System.Drawing.Font("微软雅黑", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.label3.Location = new System.Drawing.Point(54, 138);
+            this.label3.Location = new System.Drawing.Point(48, 222);
             this.label3.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
             this.label3.Name = "label3";
             this.label3.Size = new System.Drawing.Size(133, 38);
             this.label3.TabIndex = 19;
             this.label3.Text = "设备选择";
             // 
-            // button1
-            // 
-            this.button1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.button1.Location = new System.Drawing.Point(692, 479);
-            this.button1.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
-            this.button1.Name = "button1";
-            this.button1.Size = new System.Drawing.Size(128, 56);
-            this.button1.TabIndex = 21;
-            this.button1.Text = "OK音频";
-            this.button1.UseVisualStyleBackColor = true;
-            this.button1.Click += new System.EventHandler(this.button1_Click);
-            // 
-            // button2
-            // 
-            this.button2.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
-            this.button2.Location = new System.Drawing.Point(692, 556);
-            this.button2.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
-            this.button2.Name = "button2";
-            this.button2.Size = new System.Drawing.Size(128, 56);
-            this.button2.TabIndex = 22;
-            this.button2.Text = "NG音频";
-            this.button2.UseVisualStyleBackColor = true;
-            this.button2.Click += new System.EventHandler(this.button2_Click);
-            // 
             // AutoAnalysisXml
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(1654, 640);
-            this.Controls.Add(this.button2);
-            this.Controls.Add(this.button1);
+            this.ClientSize = new System.Drawing.Size(1443, 640);
             this.Controls.Add(this.Device);
             this.Controls.Add(this.label3);
             this.Controls.Add(this.BatchAnalysus);
@@ -303,9 +243,6 @@
             this.Controls.Add(this.Master);
             this.Controls.Add(this.label4);
             this.Controls.Add(this.MakeCode_lable);
-            this.Controls.Add(this.ChooseBackUpFolder);
-            this.Controls.Add(this.BackUpFolderPath);
-            this.Controls.Add(this.label2);
             this.Controls.Add(this.Clean);
             this.Controls.Add(this.OperateResult);
             this.Controls.Add(this.ChooseFolder);
@@ -316,7 +253,7 @@
             this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3);
             this.MaximizeBox = false;
             this.Name = "AutoAnalysisXml";
-            this.Text = "UAS自动过站解析器";
+            this.Text = "UAS自动过站解析器 2022-04-24";
             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
             this.Load += new System.EventHandler(this.Form1_Load);
             ((System.ComponentModel.ISupportInitialize)(this.XmlWatcher)).EndInit();
@@ -336,9 +273,6 @@
         private System.Windows.Forms.Button ChooseFolder;
         private System.Windows.Forms.RichTextBox OperateResult;
         private System.Windows.Forms.Button Clean;
-        private System.Windows.Forms.Button ChooseBackUpFolder;
-        private System.Windows.Forms.TextBox BackUpFolderPath;
-        private System.Windows.Forms.Label label2;
         private System.Windows.Forms.Label MakeCode_lable;
         private System.Windows.Forms.ComboBox Master;
         private System.Windows.Forms.Label label4;
@@ -348,8 +282,6 @@
         private System.Windows.Forms.Timer Timer;
         private System.Windows.Forms.ComboBox Device;
         private System.Windows.Forms.Label label3;
-        private System.Windows.Forms.Button button2;
-        private System.Windows.Forms.Button button1;
     }
 }
 

+ 154 - 264
FileWatcher/AutoAnalysisXml.cs

@@ -58,6 +58,8 @@ namespace FileWatcher
         /// </summary>
         List<string> _FileName = new List<string>();
 
+        ftpOperater ftp = new ftpOperater();
+
         public AutoAnalysisXml()
         {
             tipform = new Tip();
@@ -93,9 +95,9 @@ namespace FileWatcher
             //获取缓存信息
             try
             {
-                FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
-                BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
-                Master.Text = BaseUtil.GetCacheData("Master").ToString();
+                //FolderPath.Text = BaseUtil.GetCacheData("FolderPath").ToString();
+                //BackUpFolderPath.Text = BaseUtil.GetCacheData("BackUpFolderPath").ToString();
+                //Master.Text = BaseUtil.GetCacheData("Master").ToString();
                 //AutoStart.Checked = (bool)BaseUtil.GetCacheData("AutoStart");
             }
             catch (Exception ex) { MessageBox.Show(ex.Message); }
@@ -103,7 +105,7 @@ namespace FileWatcher
             {
                 if (Master.Text == DB.Rows[i]["ma_user"].ToString())
                 {
-                    DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.10.10)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
+                    DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=DGW;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=81.71.42.91)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
                     dh = new DataHelper();
                 }
             }
@@ -124,9 +126,9 @@ namespace FileWatcher
 
         private void StartWatch_Click(object sender, EventArgs e)
         {
-            if (FolderPath.Text == "" || BackUpFolderPath.Text == "")
+            if (FolderPath.Text == "")
             {
-                OperateResult.AppendText("请选择监控文件夹和备份文件夹\n");
+                OperateResult.AppendText("请选择监控文件夹\n");
                 return;
             }
             else
@@ -136,51 +138,35 @@ namespace FileWatcher
                     OperateResult.AppendText("监控文件夹不存在\n");
                     return;
                 }
-                if (!Directory.Exists(BackUpFolderPath.Text))
-                {
-                    OperateResult.AppendText("备份文件夹不存在\n");
-                    return;
-                }
-            }
-            if (FolderPath.Text == BackUpFolderPath.Text)
-            {
-                OperateResult.AppendText("监控文件夹和备份文件夹不能相同\n");
-                return;
             }
             for (int i = 0; i < DB.Rows.Count; i++)
             {
                 if (Master.Text == DB.Rows[i]["ma_user"].ToString())
                 {
-                    DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=" + DB.Rows[i]["ma_user"].ToString() + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.10.10)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
+                    DataHelper.DBConnectionString = "Connection Timeout=0;Pooling=false;Password=select!#%*(;User ID=DGW;Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=81.71.42.91)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
                     dh = new DataHelper();
                     LogicHandler.dh = new DataHelper();
                 }
             }
             //存在工单必须是已下放状态
-            if (!dh.CheckExist("make", "ma_statuscode='STARTED' and ma_code='" + ma_code.Text + "'") && ma_code.Text != "" && Device.Text == "AOI设备")
+            if ((!dh.CheckExist("make", "ma_statuscode='STARTED' and ma_code='" + ma_code.Text + "'") || ma_code.Text == "") && Device.Text == "AOI设备")
             {
                 OperateResult.AppendText("工单不存在或者未下放\n");
                 return;
             }
-            if (!dh.CheckExist("make", "ma_code='" + ma_code.Text + "'") && ma_code.Text != "" && Device.Text == "选焊设备")
-            {
-                OperateResult.AppendText("工单不存\n");
-                return;
-            }
             XmlWatcher.Path = FolderPath.Text;
             XmlWatcher.Filter = "*.txt";
             XmlWatcher.EnableRaisingEvents = true;
             //设置缓存数据
-            BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
-            BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
-            BaseUtil.SetCacheData("Master", Master.Text);
-            BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
+            //BaseUtil.SetCacheData("FolderPath", FolderPath.Text);
+            //BaseUtil.SetCacheData("BackUpFolderPath", BackUpFolderPath.Text);
+            //BaseUtil.SetCacheData("Master", Master.Text);
+            //BaseUtil.SetCacheData("AutoStart", AutoStart.Checked);
             //设置按钮不可点击
             StartWatch.Enabled = false;
             ChooseFolder.Enabled = false;
             Master.Enabled = false;
             ma_code.Enabled = false;
-            ChooseBackUpFolder.Enabled = false;
             StopWatch.Enabled = true;
             OperateResult.AppendText("开始执行监控\n");
         }
@@ -231,242 +217,178 @@ namespace FileWatcher
             {
                 StreamReader sR = File.OpenText(FileName);
 
-                string[] data1 = null;
+                DataTable filedt = new DataTable();
+                //文件内的行,用一个DataTable存储
+                int Rowindex = 0;
                 while ((nextLine = sR.ReadLine()) != null)
                 {
-                    data1 = nextLine.Split(';');
-                }
-                string SoftVersion = data1[0];
-                List<string[]> dic = new List<string[]>();
-                List<string> sndata = new List<string>();
-                for (int i = 0; i < data1.Length; i++)
-                {
-                    //如果软件版本相同的时候,则表示一个新的SN
-                    if ((SoftVersion == data1[i] && i != 0) || i == data1.Length - 1)
-                    {
-                        dic.Add(sndata.ToArray());
-                        sndata.Clear();
-                    }
-                    sndata.Add(data1[i]);
-                }
-                foreach (string[] content in dic)
-                {
-                    string SN = "";
-                    string Machine = "";
-                    string EmName = "";
-                    string Day = "";
-                    string Time = "";
-                    string Result = "";
-                    string TotalPart = "";
-                    string NGPart = "";
-                    int MissTest = 0;
-                    string[] data = content;
-                    for (int i = 0; i < data.Length; i++)
+                    //DataTable用第一行的数据作为列名
+                    string[] title = nextLine.Split(',');
+                    DataRow dr = filedt.NewRow();
+                    for (int i = 0; i < title.Length; i++)
                     {
-                        switch (i.ToString())
+                        if (Rowindex == 0)
                         {
-                            case "0":
-                                SoftVersion = data[i];
-                                break;
-                            case "1":
-                                SN = data[i];
-                                break;
-                            case "2":
-                                Machine = data[i];
-                                break;
-                            case "4":
-                                EmName = data[i];
-                                break;
-                            case "6":
-                                Day = data[i];
-                                break;
-                            case "7":
-                                Time = data[i];
-                                break;
-                            case "8":
-                                Result += data[i];
-                                break;
-                            case "9":
-                                Result += data[i];
-                                break;
-                            case "10":
-                                Result += data[i];
-                                break;
-                            case "11":
-                                Result += data[i];
-                                break;
-                            case "13":
-                                TotalPart = data[i];
-                                break;
-                            case "14":
-                                NGPart = data[i];
-                                break;
-                            default:
-                                break;
+                            filedt.Columns.Add(title[i]);
+                        }
+                        else
+                        {
+                            dr[filedt.Columns[i].ColumnName] = title[i];
                         }
                     }
-                    if (SN == "")
+                    //除了第一行,然后添加到表格中
+                    if (Rowindex > 0)
                     {
-                        tipform.startthread("序列号不能为空", "NG");
-                        continue;
+                        filedt.Rows.Add(dr);
                     }
-                    if (data.Length > 15)
+                    Rowindex = Rowindex + 1;
+                }
+                sR.Close();
+                string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1).Replace("-", "/").ToUpper().Replace(".TXT", "");
+                string Result = "OK";
+                string makecode = "";
+                string stepcode = "";
+                string oErrMessage = "";
+                string oMSID = "";
+                isource = "SMT_01_AOI";
+                iusercode = "SMT_01_AOI";
+                DataTable dt = (DataTable)dh.ExecuteSql("select sc_linecode,sc_stepcode,st_badgroupcode from source left join step on sc_stepcode=st_code where sc_code='" + isource + "'", "select");
+                if (dt.Rows.Count > 0)
+                {
+                    ilinecode = dt.Rows[0]["sc_linecode"].ToString();
+                    istepcode = dt.Rows[0]["sc_stepcode"].ToString();
+                    ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
+                }
+                if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, isource, SN, iusercode, out makecode, out oMSID, out oErrMessage))
+                {
+                    for (int i = 0; i < filedt.Rows.Count; i++)
                     {
-                        for (int i = 15; i < data.Length; i++)
+                        if (filedt.Rows[i]["c_result"].ToString() != "OK" && filedt.Rows[i]["c_result"].ToString() != "FC")
                         {
-                            switch ((i - 15) % 6)
+                            Result = "NG";
+                            //只添加一条记录
+                            if (badcode.Count == 0)
                             {
-                                case 0:
-                                    if (badlocation.Count >= 1)
-                                    {
-                                        continue;
-                                    }
-                                    badlocation.Add(data[i]);
-                                    break;
-                                case 1:
-                                    if (badprod.Count >= 1)
-                                    {
-                                        continue;
-                                    }
-                                    badprod.Add(data[i]);
-                                    break;
-                                case 2:
-                                    if (badcode.Count >= 1)
-                                    {
-                                        continue;
-                                    }
-                                    badcode.Add(data[i].ToUpper());
-                                    break;
-                                default:
-                                    break;
+                                badcode.Add(filedt.Rows[i]["w_ng_name"].ToString());
+                                badlocation.Add(filedt.Rows[i]["c_subboard_number"].ToString());
+                                badprod.Add(filedt.Rows[i]["c_subboard_number"].ToString());
                             }
                         }
                     }
-                    sR.Close();
-                    //如果没有不良位号等数据则是15个数据
-                    if (Result == "0001")
+                    //获取序列号ID最大的工单
+                    dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id,ms_stepcode from makeserial where ms_sncode='" + SN + "' order by ms_id desc", "select");
+                    if (dt.Rows.Count > 0)
                     {
-                        if (data.Length > 16)
+                        ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
+                        makecode = dt.Rows[0]["ms_makecode"].ToString();
+                        stepcode = dt.Rows[0]["ms_stepcode"].ToString();
+                        if (stepcode == istepcode)
                         {
-                            MissTest = -1;
-                            Result = "NG";
+                            tipform.startthread("序列号 " + SN + "已执行过" + istepcode, "NG");
+                            OperateResult.AppendText("序列号 " + SN + "已执行过" + istepcode + "\n");
                         }
-                        else
+                        switch (Result)
                         {
-                            Result = "OK";
+                            case "OK":
+                                if (!LogicHandler.SetStepResult(makecode, isource, SN, "自动过站采集", Result, iusercode, out oErrMessage))
+                                {
+                                    OperateResult.AppendText(oErrMessage + "\n");
+                                    tipform.startthread(oErrMessage, Result);
+                                }
+                                tipform.startthread("序列号 " + SN + "通过检测", Result);
+                                OperateResult.AppendText("序列号 " + SN + "通过检测" + "\n");
+                                break;
+                            case "NG":
+                                RecordBadInfo(SN, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), badprod.ToArray());
+                                //所有的序列号均采集为良品
+                                if (!LogicHandler.SetTestNGDetail(SN, makecode, iusercode, isource, Result, out oErrMessage))
+                                {
+                                    OperateResult.AppendText(oErrMessage + "\n");
+                                    tipform.startthread(oErrMessage, Result);
+                                }
+                                tipform.startthread("序列号 " + SN + "未通过检测", Result);
+                                OperateResult.AppendText("序列号 " + SN + "未通过检测" + "\n");
+                                break;
+                            default:
+                                break;
                         }
+                        File.Delete(FileName);
                     }
-                    else
-                    {
-                        Result = "OK";
-                    }
-                    string makecode = "";
-                    string stepcode = "";
-                    string oErrMessage = "";
-                    string oMSID = "";
-                    if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, isource, SN, iusercode, out makecode, out oMSID, out oErrMessage))
+                }
+                else
+                {
+                    OperateResult.AppendText(oErrMessage + "\n");
+                    tipform.startthread(oErrMessage, Result);
+                    File.Delete(FileName);
+                }
+            }
+            else if (Device.Text == "测试设备")
+            {
+                try
+                {
+                    string filename = FileName;
+                    if (filename.Substring(filename.LastIndexOf(".") + 1).ToUpper() == "TXT")
                     {
-                        //获取序列号ID最大的工单
-                        dt = (DataTable)dh.ExecuteSql("select ms_makecode,ms_id,ms_stepcode from makeserial where ms_sncode='" + SN + "' order by ms_id desc", "select");
-                        if (dt.Rows.Count > 0)
+                        string folderpath = FileName.Substring(0, FileName.LastIndexOf(@"\"));
+                        string SN = FileName.Substring(FileName.LastIndexOf(@"\") + 1).Replace("-", "/").ToUpper().Replace(".TXT", "");
+                        if (SN.Length == 12)
                         {
-                            ma_code.Text = dt.Rows[0]["ms_makecode"].ToString();
-                            makecode = dt.Rows[0]["ms_makecode"].ToString();
-                            stepcode = dt.Rows[0]["ms_stepcode"].ToString();
-                            if (stepcode == istepcode)
+                            string Result = "";
+                            string makecode = ma_code.Text;
+                            string oErrMessage = "";
+                            string oMSID = "";
+                            string ms_sncode = dh.getFieldDataByCondition("makeserial", "ms_sncode", "substr(ms_sncode,0,12)='" + SN + "' order by ms_id").ToString();
+                            isource = "SMT_01_TEST01";
+                            iusercode = "SMT_01_TEST01";
+                            DataTable dt = (DataTable)dh.ExecuteSql("select sc_linecode,sc_stepcode,st_badgroupcode from source left join step on sc_stepcode=st_code where sc_code='" + isource + "'", "select");
+                            if (dt.Rows.Count > 0)
                             {
-                                tipform.startthread("序列号 " + SN + "已执行过" + istepcode, "NG");
-                                OperateResult.AppendText("序列号 " + SN + "已执行过" + istepcode + "\n");
-                                continue;
+                                ilinecode = dt.Rows[0]["sc_linecode"].ToString();
+                                istepcode = dt.Rows[0]["sc_stepcode"].ToString();
+                                ibadgroup = dt.Rows[0]["st_badgroupcode"].ToString();
                             }
-                            SoundPlayer player = new SoundPlayer();
-                            switch (Result)
+                            if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, isource, ms_sncode, iusercode, out makecode, out oMSID, out oErrMessage))
                             {
-                                case "OK":
-                                    if (!LogicHandler.SetStepResult(makecode, isource, SN, "自动过站采集", Result, iusercode, out oErrMessage))
-                                    {
-                                        OperateResult.AppendText(oErrMessage + "\n");
-                                        tipform.startthread(oErrMessage, Result);
-                                        continue;
-                                    }
-                                    tipform.startthread("序列号 " + SN + "通过检测", Result);
-                                    player.SoundLocation = Application.StartupPath + @"\8378.wav";
-                                    player.Load();
-                                    player.Play();
-                                    break;
-                                case "NG":
-                                    RecordBadInfo(SN, makecode, FileName, "", badcode.ToArray(), badlocation.ToArray(), badprod.ToArray());
-                                    //所有的序列号均采集为良品
-                                    if (!LogicHandler.SetTestNGDetail(SN, makecode, iusercode, isource, Result, out oErrMessage))
-                                    {
-                                        OperateResult.AppendText(oErrMessage + "\n");
-                                        tipform.startthread(oErrMessage, Result);
-                                        continue;
-                                    }
-                                    tipform.startthread("序列号 " + SN + "未通过检测", Result);
-                                    player.SoundLocation = Application.StartupPath + @"\5185.wav";
-                                    player.Load();
-                                    player.Play();
-                                    break;
-                                default:
-                                    break;
-                            }
-                            LogicHandler.AutoPassLog(SN, isource, makecode, Day + Time, istepcode, ilinecode, SoftVersion, Result == "NG" ? "-1" : "0", "0", MissTest, TotalPart, NGPart);
-                            FileName = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
-                            FileInfo file = new FileInfo(FolderPath.Text + @"\" + FileName);
-                            if (file.Exists)
-                            {
-                                try
+                                ma_code.Text = makecode;
+                                if (LogicHandler.SetStepResult(makecode, isource, ms_sncode, "自动过站采集", "OK", iusercode, out oErrMessage))
                                 {
-                                    for (int i = 1; i <= 20; i++)
+                                    try
                                     {
-                                        if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
+                                        string ftppath = "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
+                                        ftp.UpLoadFile(folderpath, SN + ".txt", ftppath, "");
+                                        int num = int.Parse(dh.ExecuteSql("insert into STEPTESTDETAIL (std_id,std_sn,std_makecode,std_indate,std_class)select STEPTESTDETAIL_seq.nextval,ms_sncode,ms_makecode,sysdate,'http://81.71.42.91:8099/ftp" + ftppath + SN + ".txt" + "' from makeserial where substr(ms_sncode,0,12)='" + SN + "'", "insert").ToString());
+                                        if (num > 0)
                                         {
-                                            file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
-                                            OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
-                                            break;
+                                            OperateResult.AppendText("序列号:" + SN + "上传成功\n");
                                         }
-                                        else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
+                                        else
                                         {
-                                            file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
-                                            OperateResult.AppendText("成功解析文件" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + FileName + "\n");
-                                            break;
+                                            OperateResult.AppendText("序列号: " + SN + "前工段未扫描\n");
+                                            MessageBox.Show("序列号: " + ms_sncode + "前工段未扫描", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                                         }
                                     }
+                                    catch (Exception ex)
+                                    {
+                                        Console.WriteLine(ex.Message);
+                                    }
+                                    File.Delete(FileName);
                                 }
-                                catch (Exception ex)
+                                else
                                 {
-                                    OperateResult.AppendText(FileName + ex.Message + "\n");
+                                    OperateResult.AppendText(oErrMessage + "\n");
+                                    tipform.startthread(oErrMessage, Result);
+
                                 }
+                                tipform.startthread("序列号 " + ms_sncode + "通过检测", Result);
+                            }
+                            else
+                            {
+                                File.Delete(FileName);
+                                OperateResult.AppendText(oErrMessage + "\n");
+                                tipform.startthread(oErrMessage, Result);
                             }
                         }
                     }
-                    else
-                    {
-                        OperateResult.AppendText(oErrMessage + "\n");
-                        tipform.startthread(oErrMessage, Result);
-                        continue;
-                    }
-                }
-            }
-            else if (Device.Text == "选焊设备")
-            {
-                try
-                {
-                    Dictionary<string, string> dic = new Dictionary<string, string>();
-                    BaseUtil.GetWriteInfo(FileName, out dic);
-                    string sql = "insert into AOITESTDETAIL(atd_id,atd_makecode,atd_indate,";
-                    string sql1 = " values(AOITESTDETAIL_seq.nextval,'" + ma_code.Text + "',sysdate,";
-                    foreach (var item in dic)
-                    {
-                        sql += item.Key + ",";
-                        if (item.Key.Contains("starttime") || item.Key.Contains("endtime"))
-                            sql1 += "to_date('" + item.Value + "','dd/mm/yyyy hh24:mi:ss'),";
-                        else
-                            sql1 += "'" + item.Value + "',";
-                    }
-                    sql = sql.Substring(0, sql.Length - 1) + ")" + sql1.Substring(0, sql1.Length - 1) + ")";
-                    dh.ExecuteSql(sql, "insert");
                     OperateResult.AppendText("解析成功:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n" + FileName + "\n");
                 }
                 catch (Exception e)
@@ -522,8 +444,6 @@ namespace FileWatcher
                 {
                     badlocation.Add(myReader.ReadInnerXml() + " ");
                 }
-
-
             }
             myReader.Close();
             //获取文件名的序列号,如SA123456.xml,如果开头为-表示无条码需要自动获取
@@ -659,21 +579,7 @@ namespace FileWatcher
             {
                 try
                 {
-                    for (int i = 1; i <= 20; i++)
-                    {
-                        if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName))
-                        {
-                            file.MoveTo(BackUpFolderPath.Text + @"\" + FileName);
-                            OperateResult.AppendText("成功解析文件" + FileName + "\n");
-                            break;
-                        }
-                        else if (!File.Exists(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]))
-                        {
-                            file.MoveTo(BackUpFolderPath.Text + @"\" + FileName.Split('.')[0] + "(" + i + ")" + "." + FileName.Split('.')[1]);
-                            OperateResult.AppendText("成功解析文件" + FileName + "\n");
-                            break;
-                        }
-                    }
+
                 }
                 catch (Exception ex)
                 {
@@ -690,13 +596,13 @@ namespace FileWatcher
             dh.ExecuteSql(sql.ToString(), "update");
             sql.Clear();
             sql.Append("insert into makebad(mb_id,mb_makecode,mb_mscode,mb_sncode,mb_inman,mb_indate,mb_stepcode");
-            sql.Append(",mb_sourcecode,mb_badcode,mb_bgcode,mb_badtable,mb_status,mb_badlocation,mb_soncode,mb_content) select :ID");
+            sql.Append(",mb_sourcecode,mb_badname,mb_bgcode,mb_badtable,mb_status,mb_badlocation,mb_soncode,mb_content) select :ID");
             sql.Append(",ma_code,ms_code,ms_sncode,'" + iusercode + "',sysdate,'" + istepcode + "','" + isource + "',");
-            sql.Append(":bc_code,'" + ibadgroup + "','','0',:location,:soncode,'" + nextLine + "' from make left join makeSerial on ms_makecode=ma_code ");
+            sql.Append(":bc_code,'WG','','0',:location,:soncode,'" + nextLine + "' from make left join makeSerial on ms_makecode=ma_code ");
             sql.Append("where ms_sncode='" + sncode + "' and ms_makecode='" + makecode + "'");
             dh.BatchInsert(sql.ToString(), new string[] { "ID", "bc_code", "location", "soncode" }, ID, badcode, badlocation, soncode);
             sql.Clear();
-            sql.Append("update makebad set mb_badname=(select DISTINCT bc_name from badcode where bc_code=mb_badcode),mb_bgname=(select DISTINCT bg_name from badgroup where bg_code=mb_bgcode) ");
+            sql.Append("update makebad set mb_badcode=(select DISTINCT bc_code from badcode where bc_name=mb_badcode),mb_badname=mb_badcode,mb_bgname=(select DISTINCT bg_name from badgroup where bg_code=mb_bgcode) ");
             sql.Append("where mb_id=:ID");
             dh.BatchInsert(sql.ToString(), new string[] { "ID" }, ID);
             //记录判断日志
@@ -713,7 +619,7 @@ namespace FileWatcher
             ma_code.Enabled = true;
             Master.Enabled = true;
             ChooseFolder.Enabled = true;
-            ChooseBackUpFolder.Enabled = true;
+
             StopWatch.Enabled = false;
             OperateResult.AppendText("停止执行监控\n");
         }
@@ -741,7 +647,7 @@ namespace FileWatcher
             DialogResult result = folder.ShowDialog();
             if (result == DialogResult.OK)
             {
-                BackUpFolderPath.Text = folder.SelectedPath;
+
             }
         }
 
@@ -795,21 +701,5 @@ namespace FileWatcher
         {
             dh.ExecuteSql("select sysdate from dual", "select");
         }
-
-        private void button1_Click(object sender, EventArgs e)
-        {
-            SoundPlayer player = new SoundPlayer();
-            player.SoundLocation = Application.StartupPath + @"\8378.wav";
-            player.Load();
-            player.Play();
-        }
-
-        private void button2_Click(object sender, EventArgs e)
-        {
-            SoundPlayer player = new SoundPlayer();
-            player.SoundLocation = Application.StartupPath + @"\5185.wav";
-            player.Load();
-            player.Play();
-        }
     }
 }

+ 1 - 11
FileWatcher/DataHelper.cs

@@ -811,23 +811,13 @@ namespace FileWatcher
             command = new OracleCommand(sql, connection);
             Reconnect(command);
             command.ArrayBindCount = names[1].Length;
-
             //因为第一个数组保存的是参数的名称,所以循环从1而不是0开始
             //将第一个数组的下标固定为0作为循环添加的参数的名称
             for (int i = 1; i <= names[0].Length; i++)
             {
                 command.Parameters.Add(new OracleParameter(names[0][i - 1].ToString(), OracleDbType.Varchar2, names[i], ParameterDirection.Input));
             }
-            try
-            {
-                command.ExecuteNonQuery();
-            }
-            catch (Exception)
-            {
-                command.Connection = new OracleConnection(DBConnectionString);
-                command.Connection.Open();
-                command.ExecuteNonQuery();
-            }
+            command.ExecuteNonQuery();
             command.Dispose();
         }
 

+ 15 - 15
FileWatcher/Form2.Designer.cs

@@ -40,11 +40,11 @@
             this.label1.BackColor = System.Drawing.SystemColors.Menu;
             this.label1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.label1.ForeColor = System.Drawing.Color.Black;
-            this.label1.Location = new System.Drawing.Point(13, 10);
-            this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
-            this.label1.MaximumSize = new System.Drawing.Size(400, 0);
+            this.label1.Location = new System.Drawing.Point(20, 16);
+            this.label1.Margin = new System.Windows.Forms.Padding(6, 0, 6, 0);
+            this.label1.MaximumSize = new System.Drawing.Size(600, 0);
             this.label1.Name = "label1";
-            this.label1.Size = new System.Drawing.Size(0, 27);
+            this.label1.Size = new System.Drawing.Size(0, 41);
             this.label1.TabIndex = 0;
             // 
             // checkBox1
@@ -53,10 +53,10 @@
             this.checkBox1.BackColor = System.Drawing.SystemColors.Menu;
             this.checkBox1.Font = new System.Drawing.Font("宋体", 7.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
             this.checkBox1.ForeColor = System.Drawing.Color.Black;
-            this.checkBox1.Location = new System.Drawing.Point(13, 68);
-            this.checkBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+            this.checkBox1.Location = new System.Drawing.Point(20, 138);
+            this.checkBox1.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
             this.checkBox1.Name = "checkBox1";
-            this.checkBox1.Size = new System.Drawing.Size(107, 17);
+            this.checkBox1.Size = new System.Drawing.Size(161, 27);
             this.checkBox1.TabIndex = 1;
             this.checkBox1.Text = "是否自动隐藏";
             this.checkBox1.UseVisualStyleBackColor = false;
@@ -66,10 +66,10 @@
             // 
             this.panel1.Controls.Add(this.checkBox1);
             this.panel1.Controls.Add(this.label1);
-            this.panel1.Location = new System.Drawing.Point(3, 0);
-            this.panel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+            this.panel1.Location = new System.Drawing.Point(4, 0);
+            this.panel1.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
             this.panel1.Name = "panel1";
-            this.panel1.Size = new System.Drawing.Size(413, 91);
+            this.panel1.Size = new System.Drawing.Size(620, 178);
             this.panel1.TabIndex = 2;
             this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
             this.panel1.MouseEnter += new System.EventHandler(this.Form2_MouseEnter);
@@ -77,21 +77,21 @@
             // 
             // Tip
             // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
+            this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.BackColor = System.Drawing.SystemColors.Menu;
-            this.ClientSize = new System.Drawing.Size(416, 98);
+            this.ClientSize = new System.Drawing.Size(624, 193);
             this.Controls.Add(this.panel1);
             this.ForeColor = System.Drawing.Color.White;
             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
-            this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
+            this.Margin = new System.Windows.Forms.Padding(6, 6, 6, 6);
             this.Name = "Tip";
             this.Text = "Form2";
             this.TopMost = true;
             this.Load += new System.EventHandler(this.Form2_Load);
-            this.panel1.ResumeLayout(true);
+            this.panel1.ResumeLayout(false);
             this.panel1.PerformLayout();
-            this.ResumeLayout(true);
+            this.ResumeLayout(false);
 
         }
 

+ 112 - 7
FileWatcher/Program.cs

@@ -1,7 +1,10 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
+using Microsoft.Win32;
+using System;
+using System.IO;
+using System.Security.Principal;
+using System.Text;
 using System.Windows.Forms;
+using System.Xml;
 
 namespace FileWatcher
 {
@@ -13,9 +16,111 @@ namespace FileWatcher
         [STAThread]
         static void Main()
         {
-            Application.EnableVisualStyles();
-            Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new 测试记录解析DCW());
+            try
+            {
+                WindowsIdentity identity = WindowsIdentity.GetCurrent();
+                WindowsPrincipal principal = new WindowsPrincipal(identity);
+                string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
+                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
+                //处理UI线程异常
+                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
+                //处理非UI线程异常
+                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
+                if (!Directory.Exists(AutoAnalysisXml.CachePathFolder))
+                    Directory.CreateDirectory(AutoAnalysisXml.CachePathFolder);
+                FileStream fcaches = new FileStream(AutoAnalysisXml.CachePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
+                fcaches.Close();
+                FileInfo info = new FileInfo(AutoAnalysisXml.CachePath);
+                if (info.Length == 0)
+                {
+                    XmlDocument doc = new XmlDocument();
+                    //创建类型声明节点  
+                    XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
+                    doc.AppendChild(node);
+                    //创建根节点  
+                    XmlElement xeRoot = doc.CreateElement("cacheInfo");
+                    doc.AppendChild(xeRoot);
+                    doc.Save(AutoAnalysisXml.CachePath);
+                }
+                Application.EnableVisualStyles();
+                Application.SetCompatibleTextRenderingDefault(false);
+                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
+                    Application.Run(new AutoAnalysisXml());
+                else
+                {
+                    //创建启动对象
+                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
+                    // 设置运行文件
+                    startInfo.FileName = Application.ExecutablePath;
+                    //设置启动动作,确保以管理员身份运行
+                    startInfo.Verb = "runas";
+                    //如果不是管理员,则启动UAC
+                    System.Diagnostics.Process.Start(startInfo);
+                }
+            }
+            catch (Exception)
+            {
+
+            }
+        }
+
+        public static void SetAutoRun(string fileName, bool isAutoRun)
+        {
+            RegistryKey reg = null;
+            try
+            {
+                if (!System.IO.File.Exists(fileName))
+                    throw new Exception("该文件不存在!");
+                string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
+                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
+                if (reg == null)
+                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
+                if (isAutoRun)
+                    reg.SetValue(name, fileName);
+                else
+                    reg.SetValue(name, false);
+            }
+            catch (Exception ex)
+            {
+                throw new Exception(ex.ToString());
+            }
+            finally
+            {
+                if (reg != null)
+                    reg.Close();
+            }
+        }
+        //处理线程的异常
+        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
+        {
+            string str = GetExceptionMsg(e.Exception, e.ToString());
+            MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
+        }
+
+        //未处理的异常统一通过这里返回
+        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+        {
+            string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
+            MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
+        }
+
+        /// <summary>
+        /// 生成自定义异常消息
+        /// </summary>
+        /// <param name="ex">异常对象</param>
+        /// <param name="backStr">备用异常消息:当ex为null时有效</param>
+        /// <returns>异常字符串文本</returns>
+        static string GetExceptionMsg(Exception ex, string backStr)
+        {
+            StringBuilder sb = new StringBuilder();
+            if (ex != null)
+            {
+                sb.AppendLine(ex.Message);
+                //sb.AppendLine("【异常方法】:" + ex.StackTrace);
+            }
+            //else { sb.AppendLine("【未处理异常】:" + backStr); }
+            return sb.ToString();
         }
     }
-}
+
+}