Program.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 UploadMakePlan());
  49. //Application.Run(new SOP("", ""));
  50. //Application.Run(new AutoMakeQTY());
  51. }
  52. else
  53. {
  54. //创建启动对象
  55. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  56. // 设置运行文件
  57. startInfo.FileName = Application.ExecutablePath;
  58. //设置启动动作,确保以管理员身份运行
  59. startInfo.Verb = "runas";
  60. //如果不是管理员,则启动UAC
  61. System.Diagnostics.Process.Start(startInfo);
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. }
  67. }
  68. public static void SetAutoRun(string fileName, bool isAutoRun)
  69. {
  70. RegistryKey reg = null;
  71. try
  72. {
  73. if (!System.IO.File.Exists(fileName))
  74. throw new Exception("该文件不存在!");
  75. string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
  76. reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  77. if (reg == null)
  78. reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  79. if (isAutoRun)
  80. reg.SetValue(name, fileName);
  81. else
  82. reg.SetValue(name, false);
  83. }
  84. catch (Exception ex)
  85. {
  86. throw new Exception(ex.ToString());
  87. }
  88. finally
  89. {
  90. if (reg != null)
  91. reg.Close();
  92. }
  93. }
  94. //处理线程的异常
  95. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  96. {
  97. string str = GetExceptionMsg(e.Exception, e.ToString());
  98. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  99. }
  100. //未处理的异常统一通过这里返回
  101. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  102. {
  103. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  104. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  105. }
  106. /// <summary>
  107. /// 生成自定义异常消息
  108. /// </summary>
  109. /// <param name="ex">异常对象</param>
  110. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  111. /// <returns>异常字符串文本</returns>
  112. static string GetExceptionMsg(Exception ex, string backStr)
  113. {
  114. StringBuilder sb = new StringBuilder();
  115. if (ex != null)
  116. {
  117. sb.AppendLine(ex.Message);
  118. sb.AppendLine("【异常方法】:" + ex.StackTrace);
  119. }
  120. else { sb.AppendLine("【未处理异常】:" + backStr); }
  121. return sb.ToString();
  122. }
  123. }
  124. }