Program.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Entity;
  8. using UAS_PLCDataReader.PublicMethod;
  9. namespace UAS_PLCDataReader
  10. {
  11. static class Program
  12. {
  13. /// <summary>
  14. /// 应用程序的主入口点
  15. /// </summary>
  16. [STAThread]
  17. static void Main()
  18. {
  19. try
  20. {
  21. WindowsIdentity identity = WindowsIdentity.GetCurrent();
  22. WindowsPrincipal principal = new WindowsPrincipal(identity);
  23. //设置应用程序处理异常方式:ThreadException处理
  24. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  25. //处理UI线程异常
  26. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  27. //处理非UI线程异常
  28. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  29. #region 应用程序的主入口点
  30. Application.EnableVisualStyles();
  31. Application.SetCompatibleTextRenderingDefault(false);
  32. //处理全局事件
  33. GlobalEventsHandler g = new GlobalEventsHandler();
  34. //添加全局事件的监听
  35. Application.AddMessageFilter(g);
  36. //启用异常记录日志的操作
  37. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  38. //创建标签缓存的文件夹
  39. if (!Directory.Exists(LogManager.LogAddress))
  40. Directory.CreateDirectory(LogManager.LogAddress);
  41. FileStream fs = new FileStream(LogManager.LogAddress + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  42. fs.Close();
  43. DevExpress.Skins.SkinManager.EnableFormSkins();
  44. if (!Directory.Exists(SystemInf.CacheFolder))
  45. Directory.CreateDirectory(SystemInf.CacheFolder);
  46. FileStream fcaches = new FileStream(SystemInf.CacheFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  47. fcaches.Close();
  48. //判断是否xml文件大小为0
  49. FileInfo info = new FileInfo(SystemInf.CacheFilePath);
  50. if (info.Length == 0)
  51. {
  52. XmlDocument doc = new XmlDocument();
  53. //创建类型声明节点
  54. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  55. doc.AppendChild(node);
  56. //创建根节点
  57. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  58. doc.AppendChild(xeRoot);
  59. doc.Save(SystemInf.CacheFilePath);
  60. }
  61. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  62. Application.Run(new Login());
  63. else
  64. {
  65. //创建启动对象
  66. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  67. // 设置运行文件
  68. startInfo.FileName = Application.ExecutablePath;
  69. //设置启动动作,确保以管理员身份运行
  70. startInfo.Verb = "runas";
  71. //如果不是管理员,则启动UAC
  72. System.Diagnostics.Process.Start(startInfo);
  73. }
  74. #endregion
  75. }
  76. catch (Exception ex)
  77. {
  78. string str = GetExceptionMsg(ex, string.Empty);
  79. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  80. }
  81. }
  82. //处理线程的异常
  83. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  84. {
  85. string str = GetExceptionMsg(e.Exception, e.ToString());
  86. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  87. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  88. }
  89. //未处理的异常统一通过这里返回
  90. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  91. {
  92. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  93. LogManager.DoLog(str);
  94. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  95. }
  96. /// <summary>
  97. /// 生成自定义异常消息
  98. /// </summary>
  99. /// <param name="ex">异常对象</param>
  100. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  101. /// <returns>异常字符串文本</returns>
  102. static string GetExceptionMsg(Exception ex, string backStr)
  103. {
  104. StringBuilder sb = new StringBuilder();
  105. if (ex != null)
  106. {
  107. sb.AppendLine(ex.Message);
  108. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  109. }
  110. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  111. return sb.ToString();
  112. }
  113. }
  114. }