Program.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Security.Principal;
  5. using UAS_LabelMachine.PublicMethod;
  6. using UAS_Labeling.PublicMethod;
  7. namespace UAS_LabelMachine
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. static void Main(string[] Args)
  16. {
  17. try
  18. {
  19. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  20. WindowsPrincipal principal = new WindowsPrincipal(identity);
  21. //设置应用程序处理异常方式:ThreadException处理
  22. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  23. //处理UI线程异常
  24. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  25. //处理非UI线程异常
  26. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  27. #region 应用程序的主入口点
  28. Application.EnableVisualStyles();
  29. Application.SetCompatibleTextRenderingDefault(false);
  30. //启用异常记录日志的操作
  31. GlobalEventsHandler g = new GlobalEventsHandler();
  32. //添加全局事件的监听
  33. Application.AddMessageFilter(g);
  34. //如果是管理员的身份
  35. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  36. {
  37. Application.Run(new Login());
  38. }
  39. else
  40. {
  41. //创建启动对象
  42. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  43. // 设置运行文件
  44. startInfo.FileName = Application.ExecutablePath;
  45. //设置启动动作,确保以管理员身份运行
  46. startInfo.Verb = "runas";
  47. //如果不是管理员,则启动UAC
  48. System.Diagnostics.Process.Start(startInfo);
  49. //退出 System.Windows.Forms.Application.Exit();
  50. }
  51. #endregion
  52. }
  53. catch (Exception ex)
  54. {
  55. string str = GetExceptionMsg(ex, string.Empty);
  56. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  57. }
  58. }
  59. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  60. {
  61. string str = GetExceptionMsg(e.Exception, e.ToString());
  62. LogManager.DoLog(e.Exception.StackTrace);
  63. MessageBox.Show(str, "操作异常", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  64. }
  65. //未处理的异常统一通过这里返回
  66. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  67. {
  68. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  69. MessageBox.Show(str, "操作异常", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  70. }
  71. /// <summary>
  72. /// 生成自定义异常消息
  73. /// </summary>
  74. /// <param name="ex">异常对象</param>
  75. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  76. /// <returns>异常字符串文本</returns>
  77. static string GetExceptionMsg(Exception ex, string backStr)
  78. {
  79. StringBuilder sb = new StringBuilder();
  80. if (ex != null)
  81. {
  82. sb.AppendLine(ex.Message);
  83. LogManager.DoLog(ex.StackTrace);
  84. LogManager.DoLog(ex.TargetSite.ToString());
  85. //sb.AppendLine(ex.StackTrace);
  86. //sb.AppendLine("【异常方法】:" + ex.TargetSite);
  87. }
  88. LogManager.DoLog(sb.ToString());
  89. return sb.ToString();
  90. }
  91. }
  92. }