Program.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Principal;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace UAS_CheckWork
  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. //设置应用程序处理异常方式:ThreadException处理
  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. #region 应用程序的主入口点
  29. Application.EnableVisualStyles();
  30. Application.SetCompatibleTextRenderingDefault(false);
  31. ////处理全局事件
  32. //GlobalEventsHandler g = new GlobalEventsHandler();
  33. //添加全局事件的监听
  34. //Application.AddMessageFilter(g);
  35. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  36. Application.Run(new Form1());
  37. else
  38. {
  39. //创建启动对象
  40. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  41. // 设置运行文件
  42. startInfo.FileName = Application.ExecutablePath;
  43. //设置启动动作,确保以管理员身份运行
  44. startInfo.Verb = "runas";
  45. //如果不是管理员,则启动UAC
  46. System.Diagnostics.Process.Start(startInfo);
  47. }
  48. #endregion
  49. }
  50. catch (Exception ex)
  51. {
  52. string str = GetExceptionMsg(ex, string.Empty);
  53. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  54. }
  55. }
  56. //处理线程的异常
  57. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  58. {
  59. string str = GetExceptionMsg(e.Exception, e.ToString());
  60. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  61. }
  62. //未处理的异常统一通过这里返回
  63. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  64. {
  65. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  66. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  67. }
  68. /// <summary>
  69. /// 生成自定义异常消息
  70. /// </summary>
  71. /// <param name="ex">异常对象</param>
  72. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  73. /// <returns>异常字符串文本</returns>
  74. static string GetExceptionMsg(Exception ex, string backStr)
  75. {
  76. StringBuilder sb = new StringBuilder();
  77. if (ex != null)
  78. {
  79. sb.AppendLine(ex.Message);
  80. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  81. }
  82. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  83. return sb.ToString();
  84. }
  85. }
  86. }