Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.IO;
  3. using System.Security.Principal;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using UAS_MES.PublicMethod;
  7. namespace UAS_MES
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. try
  18. {
  19. Console.WriteLine("1231");
  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. //启用异常记录日志的操作
  36. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  37. //创建标签缓存的文件夹
  38. if (!Directory.Exists(ftpOperater.DownLoadTo))
  39. Directory.CreateDirectory(ftpOperater.DownLoadTo);
  40. if (!Directory.Exists(sysdisc + @":\Log"))
  41. Directory.CreateDirectory(sysdisc + @":\Log");
  42. FileStream fs = new FileStream(sysdisc + @":\Log\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  43. fs.Close();
  44. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  45. Application.Run(new Login());
  46. else
  47. {
  48. Console.WriteLine("Test");
  49. //创建启动对象
  50. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  51. // 设置运行文件
  52. startInfo.FileName = Application.ExecutablePath;
  53. //设置启动动作,确保以管理员身份运行
  54. startInfo.Verb = "runas";
  55. //如果不是管理员,则启动UAC
  56. System.Diagnostics.Process.Start(startInfo);
  57. }
  58. #endregion
  59. }
  60. catch (Exception ex)
  61. {
  62. string str = GetExceptionMsg(ex, string.Empty);
  63. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  64. }
  65. }
  66. //处理线程的异常
  67. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  68. {
  69. string str = GetExceptionMsg(e.Exception, e.ToString());
  70. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  71. MessageBox.Show(str, "操作异常", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  72. }
  73. //未处理的异常统一通过这里返回
  74. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  75. {
  76. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  77. LogManager.DoLog(str);
  78. MessageBox.Show(str, "操作异常", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  79. }
  80. /// <summary>7
  81. /// 生成自定义异常消息
  82. /// </summary>
  83. /// <param name="ex">异常对象</param>
  84. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  85. /// <returns>异常字符串文本</returns>
  86. static string GetExceptionMsg(Exception ex, string backStr)
  87. {
  88. StringBuilder sb = new StringBuilder();
  89. if (ex != null)
  90. {
  91. sb.AppendLine(ex.Message);
  92. sb.AppendLine("【异常方法】:" + ex.TargetSite);
  93. }
  94. else { sb.AppendLine("【未处理异常】:" + backStr); }
  95. return sb.ToString();
  96. }
  97. }
  98. }