Program.cs 5.9 KB

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