Parcourir la source

修改文件存储路径

章政 il y a 7 ans
Parent
commit
95a36dd663
3 fichiers modifiés avec 238 ajouts et 632 suppressions
  1. 180 624
      UAS_XmlAnalysor/DataHelper.cs
  2. 14 5
      UAS_XmlAnalysor/Form1.cs
  3. 44 3
      UAS_XmlAnalysor/Program.cs

Fichier diff supprimé car celui-ci est trop grand
+ 180 - 624
UAS_XmlAnalysor/DataHelper.cs


+ 14 - 5
UAS_XmlAnalysor/Form1.cs

@@ -3,7 +3,6 @@ using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Data;
 using System.Data;
 using System.IO;
 using System.IO;
-using System.Threading;
 using System.Windows.Forms;
 using System.Windows.Forms;
 using System.Xml;
 using System.Xml;
 
 
@@ -16,6 +15,8 @@ namespace UAS_XmlAnalysor
 
 
         DataTable dt;
         DataTable dt;
 
 
+        string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
+
         public Form1()
         public Form1()
         {
         {
             InitializeComponent();
             InitializeComponent();
@@ -25,7 +26,7 @@ namespace UAS_XmlAnalysor
         {
         {
             try
             try
             {
             {
-                StreamReader sr = new StreamReader(Application.StartupPath + @"/Cache/path.txt");
+                StreamReader sr = new StreamReader(sysdisc + @":/Cache/path.txt");
                 string line;
                 string line;
                 while ((line = sr.ReadLine()) != null)
                 while ((line = sr.ReadLine()) != null)
                 {
                 {
@@ -39,7 +40,8 @@ namespace UAS_XmlAnalysor
                 BackUpFolderPath.Text = Data[1].ToString();
                 BackUpFolderPath.Text = Data[1].ToString();
                 Source.Text = Data[2].ToString();
                 Source.Text = Data[2].ToString();
                 Master.Text = Data[3].ToString();
                 Master.Text = Data[3].ToString();
-                AutoStart.Checked = (Data[4].ToString() == "True") ? true : false;
+                Console.WriteLine(Data[4].ToString());
+                AutoStart.Checked = ((Data[4].ToString() == "True") ? true : false);
                 sr.Close();
                 sr.Close();
             }
             }
             catch (Exception)
             catch (Exception)
@@ -80,8 +82,15 @@ namespace UAS_XmlAnalysor
             XmlWatcher.EnableRaisingEvents = true;
             XmlWatcher.EnableRaisingEvents = true;
             string CacheString = FolderPath.Text + "|" + BackUpFolderPath.Text + "|" + Source.Text + "|" + Master.Text + "|" + AutoStart.Checked;
             string CacheString = FolderPath.Text + "|" + BackUpFolderPath.Text + "|" + Source.Text + "|" + Master.Text + "|" + AutoStart.Checked;
             //写入前先删除文件
             //写入前先删除文件
-            File.Delete(Application.StartupPath + @"/Cache/path.txt");
-            StreamWriter sw = File.AppendText(Application.StartupPath + @"\Cache\path.txt");
+            try
+            {
+                File.Delete(sysdisc + @":/Cache/path.txt");
+            }
+            catch (Exception e1)
+            {
+                Console.WriteLine(e1.Message);
+            }
+            StreamWriter sw = File.AppendText(sysdisc + @":/Cache/path.txt");
             sw.WriteLine(CacheString);
             sw.WriteLine(CacheString);
             sw.Close();
             sw.Close();
             Source.Enabled = false;
             Source.Enabled = false;

+ 44 - 3
UAS_XmlAnalysor/Program.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.IO;
 using System.IO;
 using System.Linq;
 using System.Linq;
 using System.Security.Principal;
 using System.Security.Principal;
+using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Windows.Forms;
 
 
@@ -21,10 +22,18 @@ namespace UAS_XmlAnalysor
             {
             {
                 WindowsIdentity identity = WindowsIdentity.GetCurrent();
                 WindowsIdentity identity = WindowsIdentity.GetCurrent();
                 WindowsPrincipal principal = new WindowsPrincipal(identity);
                 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);
+
                 Application.EnableVisualStyles();
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
                 Application.SetCompatibleTextRenderingDefault(false);
-                if (!Directory.Exists(Application.StartupPath+@"\Cache"))
-                    Directory.CreateDirectory(Application.StartupPath + @"\Cache");
+                if (!Directory.Exists(sysdisc + @":\Cache"))
+                    Directory.CreateDirectory(sysdisc + @":\Cache");
                 if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                 if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                     Application.Run(new Form1());
                     Application.Run(new Form1());
                 else
                 else
@@ -71,5 +80,37 @@ namespace UAS_XmlAnalysor
                     reg.Close();
                     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();
+        }
     }
     }
-}
+
+}

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff