Program.cs 6.0 KB

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