Program.cs 4.4 KB

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