Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 UAS_PLCDataReader.PublicMethod;
  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. DevExpress.Skins.SkinManager.EnableFormSkins();
  39. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  40. Application.Run(new Login());
  41. else
  42. {
  43. //创建启动对象
  44. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  45. // 设置运行文件
  46. startInfo.FileName = Application.ExecutablePath;
  47. //设置启动动作,确保以管理员身份运行
  48. startInfo.Verb = "runas";
  49. //如果不是管理员,则启动UAC
  50. System.Diagnostics.Process.Start(startInfo);
  51. }
  52. #endregion
  53. }
  54. catch (Exception ex)
  55. {
  56. string str = GetExceptionMsg(ex, string.Empty);
  57. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  58. }
  59. }
  60. //处理线程的异常
  61. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  62. {
  63. string str = GetExceptionMsg(e.Exception, e.ToString());
  64. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  65. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  66. }
  67. //未处理的异常统一通过这里返回
  68. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  69. {
  70. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  71. LogManager.DoLog(str);
  72. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  73. }
  74. /// <summary>
  75. /// 生成自定义异常消息
  76. /// </summary>
  77. /// <param name="ex">异常对象</param>
  78. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  79. /// <returns>异常字符串文本</returns>
  80. static string GetExceptionMsg(Exception ex, string backStr)
  81. {
  82. StringBuilder sb = new StringBuilder();
  83. if (ex != null)
  84. {
  85. sb.AppendLine(ex.Message);
  86. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  87. }
  88. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  89. return sb.ToString();
  90. }
  91. }
  92. }