Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. string sysdisc = Environment.GetEnvironmentVariable("windir").Substring(0, 1);
  34. //创建标签缓存的文件夹
  35. if (!Directory.Exists(LogManager.LogAddress))
  36. Directory.CreateDirectory(LogManager.LogAddress);
  37. FileStream fs = new FileStream(LogManager.LogAddress + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  38. fs.Close();
  39. DevExpress.Skins.SkinManager.EnableFormSkins();
  40. FileStream fcaches = new FileStream(SystemInf.CacheFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  41. fcaches.Close();
  42. //判断是否xml文件大小为0
  43. FileInfo info = new FileInfo(SystemInf.CacheFilePath);
  44. if (info.Length == 0)
  45. {
  46. XmlDocument doc = new XmlDocument();
  47. //创建类型声明节点
  48. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  49. doc.AppendChild(node);
  50. //创建根节点
  51. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  52. doc.AppendChild(xeRoot);
  53. doc.Save(SystemInf.CacheFilePath);
  54. }
  55. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  56. Application.Run(new Login());
  57. else
  58. {
  59. //创建启动对象
  60. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  61. // 设置运行文件
  62. startInfo.FileName = Application.ExecutablePath;
  63. //设置启动动作,确保以管理员身份运行
  64. startInfo.Verb = "runas";
  65. //如果不是管理员,则启动UAC
  66. System.Diagnostics.Process.Start(startInfo);
  67. }
  68. #endregion
  69. }
  70. catch (Exception ex)
  71. {
  72. string str = GetExceptionMsg(ex, string.Empty);
  73. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. }
  75. }
  76. //处理线程的异常
  77. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  78. {
  79. string str = GetExceptionMsg(e.Exception, e.ToString());
  80. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  81. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  82. }
  83. //未处理的异常统一通过这里返回
  84. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  85. {
  86. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  87. LogManager.DoLog(str);
  88. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  89. }
  90. /// <summary>
  91. /// 生成自定义异常消息
  92. /// </summary>
  93. /// <param name="ex">异常对象</param>
  94. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  95. /// <returns>异常字符串文本</returns>
  96. static string GetExceptionMsg(Exception ex, string backStr)
  97. {
  98. StringBuilder sb = new StringBuilder();
  99. if (ex != null)
  100. {
  101. sb.AppendLine(ex.Message);
  102. //sb.AppendLine("【异常方法】:" + ex.StackTrace);
  103. }
  104. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  105. return sb.ToString();
  106. }
  107. }
  108. }