Program.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using GCAS.Code;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Configuration;
  5. using System.Diagnostics;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace GCAS
  12. {
  13. internal static class Program
  14. {
  15. /// <summary>
  16. /// 应用程序的主入口点。
  17. /// </summary>
  18. [STAThread]
  19. private static void Main()
  20. { //HSL 授权
  21. if (!HslCommunication.Authorization.SetAuthorizationCode("f562cc4c-4772-4b32-bdcd-f3e122c534e3"))
  22. {
  23. LogHelper.WriteError("HSL授权失败");
  24. return;
  25. }
  26. var autoStart = ConfigurationManager.AppSettings["autoStart"];
  27. bool.TryParse(autoStart, out bool isAutoStart);
  28. AutoStart(isAutoStart);
  29. Process instance = RunningInstance();
  30. if (instance == null)
  31. {
  32. try
  33. {
  34. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  35. //处理UI线程异常
  36. Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  37. //处理非UI线程异常
  38. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  39. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  40. var language = ConfigurationManager.AppSettings["language"];
  41. if (string.IsNullOrEmpty(language))
  42. {
  43. config.AppSettings.Settings.Add(new KeyValueConfigurationElement("language", "zh-Hans"));//增加
  44. }
  45. else
  46. {
  47. Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
  48. }
  49. Application.EnableVisualStyles();
  50. Application.SetCompatibleTextRenderingDefault(false);
  51. Application.Run(new LoginForm());
  52. }
  53. catch (Exception ex)
  54. {
  55. GetExceptionMsg(ex, string.Empty);
  56. //XtraMessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  57. }
  58. }
  59. else
  60. {
  61. HandleRunningInstance(instance);
  62. }
  63. }
  64. private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  65. {
  66. string str = GetExceptionMsg(e.Exception, e.ToString());
  67. //XtraMessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  68. LogHelper.WriteError(str);
  69. }
  70. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  71. {
  72. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  73. //XtraMessageBox.Show(str, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. LogHelper.WriteError(str);
  75. }
  76. /// <summary>
  77. /// 生成自定义异常消息
  78. /// </summary>
  79. /// <param name="ex">异常对象</param>
  80. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  81. /// <returns>异常字符串文本</returns>
  82. private static string GetExceptionMsg(Exception ex, string backStr)
  83. {
  84. StringBuilder sb = new StringBuilder();
  85. sb.AppendLine("****************************异常文本****************************");
  86. sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
  87. if (ex != null)
  88. {
  89. sb.AppendLine("【异常类型】:" + ex.GetType().Name);
  90. sb.AppendLine("【异常信息】:" + ex.Message);
  91. sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
  92. }
  93. else
  94. {
  95. sb.AppendLine("【未处理异常】:" + backStr);
  96. }
  97. sb.AppendLine("***************************************************************");
  98. return sb.ToString();
  99. }
  100. [DllImport("User32.dll", EntryPoint = "FindWindow")]
  101. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  102. /// <summary>
  103. /// 该函数设置由不同线程产生的窗口的显示状态。
  104. /// </summary>
  105. /// <param name="hWnd">窗口句柄</param>
  106. /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。</param>
  107. /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns>
  108. [DllImport("User32.dll")]
  109. private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
  110. /// <summary>
  111. /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
  112. /// </summary>
  113. /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param>
  114. /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns>
  115. [DllImport("User32.dll")]
  116. private static extern bool SetForegroundWindow(IntPtr hWnd);
  117. private const int WS_SHOWNORMAL = 1;
  118. /// <summary>
  119. /// 获取正在运行的实例,没有运行的实例返回null;
  120. /// </summary>
  121. public static Process RunningInstance()
  122. {
  123. Process current = Process.GetCurrentProcess();
  124. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  125. foreach (Process process in processes)
  126. {
  127. if (process.Id != current.Id)
  128. {
  129. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  130. {
  131. return process;
  132. }
  133. }
  134. }
  135. return null;
  136. }
  137. /// <summary>
  138. /// 显示已运行的程序。
  139. /// </summary>
  140. public static void HandleRunningInstance(Process instance)
  141. {
  142. var mainHandle = instance.MainWindowHandle;
  143. if (mainHandle == IntPtr.Zero)
  144. {
  145. mainHandle = FindWindow(null, "电子秤称重管理系统-灰渣吊");
  146. }
  147. ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示,可以注释掉
  148. SetForegroundWindow(instance.MainWindowHandle); //放到前端
  149. }
  150. /// <summary>
  151. /// 修改程序在注册表中的键值
  152. /// </summary>
  153. /// <param name="isAuto">true:开机启动,false:不开机自启</param>
  154. private static void AutoStart(bool isAuto = true, bool showinfo = true)
  155. {
  156. try
  157. {
  158. if (isAuto == true)
  159. {
  160. RegistryKey R_local = Registry.CurrentUser;
  161. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  162. R_run.SetValue("电子秤称重管理系统-垃圾吊", Application.ExecutablePath);
  163. R_run.Close();
  164. R_local.Close();
  165. }
  166. else
  167. {
  168. RegistryKey R_local = Registry.CurrentUser;
  169. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  170. R_run.DeleteValue("电子秤称重管理系统-垃圾吊", false);
  171. R_run.Close();
  172. R_local.Close();
  173. }
  174. }
  175. catch (Exception)
  176. {
  177. if (showinfo)
  178. MessageBox.Show("您需要管理员权限修改", "提示");
  179. }
  180. }
  181. }
  182. }