Program.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Text;
  3. using System.Windows.Forms;
  4. using System.Security.Principal;
  5. namespace UAS_BARCODEIO
  6. {
  7. static class Program
  8. {
  9. /// <summary>
  10. /// 应用程序的主入口点。
  11. /// </summary>
  12. [STAThread]
  13. static void Main()
  14. {
  15. try
  16. {
  17. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  18. WindowsPrincipal principal = new WindowsPrincipal(identity);
  19. //设置应用程序处理异常方式:ThreadException处理
  20. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  21. //处理UI线程异常
  22. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  23. //处理非UI线程异常
  24. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  25. #region 应用程序的主入口点
  26. Application.EnableVisualStyles();
  27. Application.SetCompatibleTextRenderingDefault(false);
  28. //启用异常记录日志的操作
  29. GlobalEventsHandler g = new GlobalEventsHandler();
  30. //添加全局事件的监听
  31. Application.AddMessageFilter(g);
  32. //如果是管理员的身份
  33. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  34. {
  35. Application.Run(new 入库条码规则解析());
  36. }
  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. //退出 System.Windows.Forms.Application.Exit();
  48. }
  49. #endregion
  50. }
  51. catch (Exception ex)
  52. {
  53. string str = GetExceptionMsg(ex, string.Empty);
  54. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  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. //sb.AppendLine("【异常方法】:" + ex.TargetSite);
  82. }
  83. return sb.ToString();
  84. }
  85. }
  86. }