Program.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. namespace UAS_MES_NEW
  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(ftpOperater.DownLoadTo))
  40. Directory.CreateDirectory(ftpOperater.DownLoadTo);
  41. if (!Directory.Exists(SystemInf.LogFolder))
  42. Directory.CreateDirectory(SystemInf.LogFolder);
  43. FileStream fs = new FileStream(SystemInf.LogFolder + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  44. fs.Close();
  45. //创建存储登录信息的文件
  46. if (!Directory.Exists(SystemInf.CacheFolder))
  47. Directory.CreateDirectory(SystemInf.CacheFolder);
  48. FileStream fcaches = new FileStream(SystemInf.CacheFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
  49. fcaches.Close();
  50. DevExpress.UserSkins.BonusSkins.Register();
  51. DevExpress.Skins.SkinManager.EnableFormSkins();
  52. DevExpress.Skins.SkinManager.EnableMdiFormSkins();
  53. //判断是否xml文件大小为0
  54. FileInfo info = new FileInfo(SystemInf.CacheFilePath);
  55. if (info.Length == 0)
  56. {
  57. XmlDocument doc = new XmlDocument();
  58. //创建类型声明节点
  59. XmlNode node = doc.CreateXmlDeclaration("1.0", "utf-8", "");
  60. doc.AppendChild(node);
  61. //创建根节点
  62. XmlElement xeRoot = doc.CreateElement("cacheInfo");
  63. doc.AppendChild(xeRoot);
  64. doc.Save(SystemInf.CacheFilePath);
  65. }
  66. //创建记录打印进程的文件
  67. FileStream fas = new FileStream(SystemInf.CacheFolder + "lblprocess.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  68. fas.Close();
  69. if (principal.IsInRole(WindowsBuiltInRole.Administrator))
  70. {
  71. //限制程序同时运行的实例数量
  72. if (!CheckInstanceLimit())
  73. return;
  74. Application.Run(new Login());
  75. }
  76. else
  77. {
  78. //创建启动对象
  79. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  80. // 设置运行文件
  81. startInfo.FileName = Application.ExecutablePath;
  82. //设置启动动作,确保以管理员身份运行
  83. startInfo.Verb = "runas";
  84. //如果不是管理员,则启动UAC
  85. System.Diagnostics.Process.Start(startInfo);
  86. }
  87. #endregion
  88. }
  89. catch (Exception ex)
  90. {
  91. string str = GetExceptionMsg(ex, string.Empty);
  92. MessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  93. }
  94. }
  95. /// <summary>
  96. /// 允许程序同时运行的最大实例数
  97. /// </summary>
  98. private const int MaxInstanceCount = 3;
  99. /// <summary>
  100. /// 检测同名进程数量,超出上限时提示并禁止启动
  101. /// </summary>
  102. /// <returns>允许启动返回 true</returns>
  103. static bool CheckInstanceLimit()
  104. {
  105. try
  106. {
  107. //GetProcessesByName 的统计结果包含当前进程本身
  108. System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(
  109. System.Diagnostics.Process.GetCurrentProcess().ProcessName);
  110. int count = processes.Length;
  111. foreach (System.Diagnostics.Process p in processes)
  112. {
  113. p.Dispose();
  114. }
  115. if (count > MaxInstanceCount)
  116. {
  117. MessageBox.Show("程序最多只能同时运行 " + MaxInstanceCount + " 个,请关闭其它窗口后再打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  118. return false;
  119. }
  120. return true;
  121. }
  122. catch (Exception)
  123. {
  124. //统计进程失败时不拦截,避免程序无法启动
  125. return true;
  126. }
  127. }
  128. //处理线程的异常
  129. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  130. {
  131. string str = GetExceptionMsg(e.Exception, e.ToString());
  132. LogManager.DoLog(e.Exception.Message + " " + e.Exception.TargetSite + " " + e.Exception.StackTrace);
  133. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  134. }
  135. //未处理的异常统一通过这里返回
  136. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  137. {
  138. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  139. LogManager.DoLog(str);
  140. MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  141. }
  142. /// <summary>
  143. /// 生成自定义异常消息
  144. /// </summary>
  145. /// <param name="ex">异常对象</param>
  146. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  147. /// <returns>异常字符串文本</returns>
  148. static string GetExceptionMsg(Exception ex, string backStr)
  149. {
  150. StringBuilder sb = new StringBuilder();
  151. if (ex != null)
  152. {
  153. sb.AppendLine(ex.Message);
  154. sb.AppendLine("【异常方法】:" + ex.StackTrace);
  155. }
  156. //else { sb.AppendLine("【未处理异常】:" + backStr); }
  157. return sb.ToString();
  158. }
  159. }
  160. }