Program.cs 5.7 KB

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