Program.cs 5.1 KB

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