Переглянути джерело

清理打印文件记录时添加异常处理

Hcsy 8 роки тому
батько
коміт
37be53b08c
1 змінених файлів з 72 додано та 47 видалено
  1. 72 47
      UAS-MES/Main.cs

+ 72 - 47
UAS-MES/Main.cs

@@ -115,42 +115,55 @@ namespace UAS_MES
         //关闭打印进程
         private static void Closelblprocess()
         {
-            //杀死全部未关闭的打印进程
-            string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
-            foreach (string line in lines)
+            try
             {
-                if (line != "")
+                //杀死全部未关闭的打印进程
+                string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
+                foreach (string line in lines)
                 {
-                    string processid = line.Split('|')[0];
-                    string lblid = line.Split('|')[1];
-                    try
-                    {
-                        if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
-                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
-                    }
-                    catch (Exception)
+                    if (line != "")
                     {
+                        string processid = line.Split('|')[0];
+                        string lblid = line.Split('|')[1];
                         try
                         {
-                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                            if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).ProcessName != System.Diagnostics.Process.GetCurrentProcess().ProcessName)
+                                System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
                         }
                         catch (Exception)
                         {
+                            try
+                            {
+                                System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                            }
+                            catch (Exception)
+                            {
+                            }
+                            FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
+                            StreamReader sr = new StreamReader(fs);
+                            String s = sr.ReadToEnd();
+                            sr.Close();
+                            fs.Close();
+                            FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
+                            StreamWriter sw = new StreamWriter(fas);
+                            sw.Write(s.Replace(line + "\r\n", ""));
+                            sw.Flush();
+                            sw.Close();
+                            fas.Close();
                         }
-                        FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
-                        StreamReader sr = new StreamReader(fs);
-                        String s = sr.ReadToEnd();
-                        sr.Close();
-                        fs.Close();
-                        FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
-                        StreamWriter sw = new StreamWriter(fas);
-                        sw.Write(s.Replace(line + "\r\n", ""));
-                        sw.Flush();
-                        sw.Close();
-                        fas.Close();
                     }
                 }
             }
+            catch
+            {
+                FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
+                StreamWriter sw = new StreamWriter(fas);
+                sw.Write("");
+                sw.Flush();
+                sw.Close();
+                fas.Close();
+            }
+
         }
 
         private void TestPrintEnable()
@@ -319,37 +332,49 @@ namespace UAS_MES
 
         private void Main_FormClosing(object sender, FormClosingEventArgs e)
         {
-            //杀死全部未关闭的打印进程
-            string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
-            foreach (string line in lines)
+            try
             {
-                if (line != "")
+                //杀死全部未关闭的打印进程
+                string[] lines = System.IO.File.ReadAllLines(SystemInf.CacheFolder + "lblprocess.txt");
+                foreach (string line in lines)
                 {
-                    string processid = line.Split('|')[0];
-                    string lblid = line.Split('|')[1];
-                    try
+                    if (line != "")
                     {
-                        if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
+                        string processid = line.Split('|')[0];
+                        string lblid = line.Split('|')[1];
+                        try
+                        {
+                            if (System.Diagnostics.Process.GetProcessById(int.Parse(processid)).Id == System.Diagnostics.Process.GetCurrentProcess().Id)
+                            {
+                                System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
+                                FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
+                                StreamReader sr = new StreamReader(fs);
+                                String s = sr.ReadToEnd();
+                                sr.Close();
+                                fs.Close();
+                                FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
+                                StreamWriter sw = new StreamWriter(fas);
+                                sw.Write(s.Replace(line + "\r\n", ""));
+                                sw.Flush();
+                                sw.Close();
+                                fas.Close();
+                            }
+                        }
+                        catch (Exception)
                         {
-                            System.Diagnostics.Process.GetProcessById(int.Parse(lblid)).Kill();
-                            FileStream fs = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Open, FileAccess.Read);
-                            StreamReader sr = new StreamReader(fs);
-                            String s = sr.ReadToEnd();
-                            sr.Close();
-                            fs.Close();
-                            FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
-                            StreamWriter sw = new StreamWriter(fas);
-                            sw.Write(s.Replace(line + "\r\n", ""));
-                            sw.Flush();
-                            sw.Close();
-                            fas.Close();
                         }
-                    }
-                    catch (Exception)
-                    {
                     }
                 }
             }
+            catch
+            {
+                FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.Create, FileAccess.ReadWrite);
+                StreamWriter sw = new StreamWriter(fas);
+                sw.Write("");
+                sw.Flush();
+                sw.Close();
+                fas.Close();
+            }
             BaseUtil.FormStepInOrOut(this, false);
         }