Program.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.IO;
  3. using System.Security.Principal;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Xml;
  7. using ClassFile;
  8. namespace UAS_PLCDataReader
  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. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  33. //创建标签缓存的文件夹
  34. if (!Directory.Exists(LogManager.LogAddress))
  35. Directory.CreateDirectory(LogManager.LogAddress);
  36. FileStream fs = new FileStream(LogManager.LogAddress + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  37. fs.Close();
  38. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  39. Application.Run(new MainWindow());
  40. else
  41. {
  42. //创建启动对象
  43. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  44. // 设置运行文件
  45. startInfo.FileName = Application.ExecutablePath;
  46. //设置启动动作,确保以管理员身份运行
  47. startInfo.Verb = "runas";
  48. //如果不是管理员,则启动UAC
  49. System.Diagnostics.Process.Start(startInfo);
  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. //处理线程的异常
  60. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  61. {
  62. string str = GetExceptionMsg(e.Exception, e.ToString());
  63. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  64. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  65. }
  66. //未处理的异常统一通过这里返回
  67. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  68. {
  69. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  70. LogManager.DoLog(str);
  71. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  72. }
  73. /// <summary>
  74. /// 生成自定义异常消息
  75. /// </summary>
  76. /// <param name="ex">异常对象</param>
  77. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  78. /// <returns>异常字符串文本</returns>
  79. static string GetExceptionMsg(Exception ex, string backStr)
  80. {
  81. StringBuilder sb = new StringBuilder();
  82. if (ex != null)
  83. {
  84. sb.AppendLine(ex.Message);
  85. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  86. }
  87. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  88. return sb.ToString();
  89. }
  90. }
  91. }