Program.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. namespace FileWatcher
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. try
  19. {
  20. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  21. WindowsPrincipal principal = new WindowsPrincipal(identity);
  22. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  23. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  24. //处理UI线程异常
  25. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  26. //处理非UI线程异常
  27. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  28. if (!Directory.Exists(AutoAnalysisXml.CachePathFolder))
  29. Directory.CreateDirectory(AutoAnalysisXml.CachePathFolder);
  30. FileStream fcaches = new FileStream(AutoAnalysisXml.CachePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  31. fcaches.Close();
  32. FileInfo info = new FileInfo(AutoAnalysisXml.CachePath);
  33. if (info.Length == 0)
  34. {
  35. XmlDocument doc = new XmlDocument();
  36. //创建类型声明节点
  37. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  38. doc.AppendChild(node);
  39. //创建根节点
  40. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  41. doc.AppendChild(xeRoot);
  42. doc.Save(AutoAnalysisXml.CachePath);
  43. }
  44. Application.EnableVisualStyles();
  45. Application.SetCompatibleTextRenderingDefault(false);
  46. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  47. {
  48. Application.Run(new AutoAnalysisDeviceKS());
  49. //Application.Run(new AutoAnalysisXmlByStep());
  50. //Application.Run(new SOP("", ""));
  51. //Application.Run(new AutoMakeQTY());
  52. }
  53. else
  54. {
  55. //创建启动对象
  56. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  57. // 设置运行文件
  58. startInfo.FileName = Application.ExecutablePath;
  59. //设置启动动作,确保以管理员身份运行
  60. startInfo.Verb = "runas";
  61. //如果不是管理员,则启动UAC
  62. System.Diagnostics.Process.Start(startInfo);
  63. }
  64. }
  65. catch (Exception)
  66. {
  67. }
  68. }
  69. public static void SetAutoRun(string fileName, bool isAutoRun)
  70. {
  71. RegistryKey reg = null;
  72. try
  73. {
  74. if (!System.IO.File.Exists(fileName))
  75. throw new Exception("该文件不存在!");
  76. string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
  77. reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  78. if (reg == null)
  79. reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  80. if (isAutoRun)
  81. reg.SetValue(name, fileName);
  82. else
  83. reg.SetValue(name, false);
  84. }
  85. catch (Exception ex)
  86. {
  87. throw new Exception(ex.ToString());
  88. }
  89. finally
  90. {
  91. if (reg != null)
  92. reg.Close();
  93. }
  94. }
  95. //处理线程的异常
  96. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  97. {
  98. string str = GetExceptionMsg(e.Exception, e.ToString());
  99. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  100. }
  101. //未处理的异常统一通过这里返回
  102. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  103. {
  104. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  105. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  106. }
  107. /// <summary>
  108. /// 生成自定义异常消息
  109. /// </summary>
  110. /// <param name="ex">异常对象</param>
  111. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  112. /// <returns>异常字符串文本</returns>
  113. static string GetExceptionMsg(Exception ex, string backStr)
  114. {
  115. StringBuilder sb = new StringBuilder();
  116. if (ex != null)
  117. {
  118. sb.AppendLine(ex.Message);
  119. sb.AppendLine("【异常方法】:" + ex.StackTrace);
  120. }
  121. else { sb.AppendLine("【未处理异常】:" + backStr); }
  122. return sb.ToString();
  123. }
  124. }
  125. }