Program.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Threading;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using System.Reflection;
  8. using System.Web;
  9. using Shell32;
  10. using System.IO;
  11. using System.Net;
  12. using System.Text;
  13. namespace US.Browser.IE
  14. {
  15. static class Program
  16. {
  17. #region 添加用户信息
  18. /// <summary>
  19. /// 应用程序的主入口点
  20. /// </summary>
  21. [STAThread]
  22. static void Main(string[] args)
  23. {
  24. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理未处理的异常
  25. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); //处理主线程ui异常
  26. Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); //所有错误的异常
  27. Process instance = _R(); //直接显示
  28. if (instance == null)
  29. {
  30. Application.EnableVisualStyles();
  31. Application.SetCompatibleTextRenderingDefault(false);
  32. Application.Run(StaticClass.Restart(Environment.CommandLine)); //Environment.CommandLine args.Length > 0 ? StaticClass.Restart(Environment.CommandLine) : StaticClass.Restart("one")
  33. }
  34. }
  35. /// <summary>
  36. /// 出主线程ui的异常
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  41. {
  42. BLL.USIE.Exception(sender, e.Exception);//错误处理
  43. }
  44. /// <summary>
  45. /// 主线程ui错误处理
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  50. {
  51. BLL.USIE.Exception(sender, (Exception)e.ExceptionObject);//错误处理
  52. }
  53. /// <summary>
  54. /// 获取正在运行的实例,没有运行的实例返回null;
  55. /// </summary>
  56. public static Process RunningInstance()
  57. {
  58. Process current = Process.GetCurrentProcess();
  59. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  60. foreach (Process process in processes)
  61. {
  62. if (process.Id != current.Id)
  63. {
  64. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  65. {
  66. return process;
  67. }
  68. }
  69. }
  70. return null;
  71. }
  72. /// <summary>
  73. /// 判断应用程序是否启动
  74. /// </summary>
  75. /// <returns></returns>
  76. private static Process _R()
  77. {
  78. Process currentproc = Process.GetCurrentProcess();
  79. Process[] processcollection = Process.GetProcessesByName(currentproc.ProcessName.Replace(".vshost", string.Empty));
  80. if (processcollection.Length >= 1)
  81. {
  82. foreach (Process process in processcollection)
  83. {
  84. if (process.Id != currentproc.Id)
  85. {
  86. if (Assembly.GetExecutingAssembly().Location.Replace(@"/", @"\") == currentproc.MainModule.FileName)
  87. {
  88. if (BLL.USIE.isnet())
  89. {
  90. StaticClass._UST = "";
  91. IntPtr formhwnd = BLL.WindowHWND.FindWindow(null, "云端");
  92. BLL.WindowHWND.SetForegroundWindow(formhwnd); //将窗口放置最前端
  93. BLL.WindowHWND.SwitchToThisWindow(formhwnd, true);
  94. BLL.WindowHWND.ShowWindow(formhwnd, BLL.WindowHWND.SW_SHOW); //调用api函数,正常显示窗口
  95. }
  96. return process;
  97. }
  98. }
  99. }
  100. }
  101. return null;
  102. }
  103. #endregion
  104. #region 无用区域
  105. ///// <summary>
  106. ///// 判断程序是否安装
  107. ///// </summary>
  108. ///// <returns></returns>
  109. //private static bool isIn()
  110. //{
  111. // object _UDS; Microsoft.Win32.RegistryKey _UEK;
  112. // Microsoft.Win32.RegistryKey _UKE = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall");//获取所有安装的程序
  113. // foreach (string USE in _UKE.GetSubKeyNames())
  114. // {
  115. // _UEK = _UKE.OpenSubKey(USE); _UDS = _UEK.GetValue("DisplayName"); if (_UDS != null)
  116. // {
  117. // if (_UDS.ToString().Contains("云端")) { return true; }
  118. // }
  119. // }
  120. // return false;
  121. //}
  122. //[STAThread]
  123. //static void Main()
  124. //{
  125. // Application.EnableVisualStyles();
  126. // Application.SetCompatibleTextRenderingDefault(false);
  127. // Application.Run(StaticClass.getInstance());
  128. //if (UD.TF) { int cfu = UD.CheckForUpdate().Count; }
  129. //}
  130. //if (UD.CFUTF())
  131. //{
  132. // try
  133. // {
  134. // Application.Run(UpdateCue.NewUpCF());
  135. // }
  136. // catch { MessageBox.Show("文件损坏~!请重新下载!"); Environment.Exit(0); }
  137. //}
  138. //else
  139. //{
  140. // Application.Run(StaticClass.getInstance());
  141. //}
  142. //Application.Run(StaticClass.getInstance());
  143. //MessageBox.Show(arg[0]);
  144. //private static Process RunningInstance()
  145. //{
  146. // Process current = Process.GetCurrentProcess();
  147. // MessageBox.Show(current.ProcessName);
  148. // Process[] processes = Process.GetProcessesByName(current.ProcessName);
  149. // //遍历与当前进程名称相同的进程列表
  150. // foreach (Process process in processes)
  151. // {
  152. // //如果实例已经存在则忽略当前进程
  153. // if (process.Id != current.Id)
  154. // {
  155. // //保证要打开的进程同已经存在的进程来自同一文件路径
  156. // if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  157. // {
  158. // //返回已经存在的进程
  159. // return process;
  160. // }
  161. // }
  162. // }
  163. // return null;
  164. //}
  165. //private static Process RunningInstance()
  166. //{
  167. // Process current = Process.GetCurrentProcess();
  168. // Process[] processes = Process.GetProcessesByName(current.ProcessName);
  169. // //遍历与当前进程名称相同的进程列表
  170. // foreach (Process process in processes)
  171. // {
  172. // //如果实例已经存在则忽略当前进程
  173. // if (process.Id != current.Id)
  174. // {
  175. // //保证要打开的进程同已经存在的进程来自同一文件路径
  176. // if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  177. // {
  178. // //返回已经存在的进程
  179. // return process;
  180. // }
  181. // }
  182. // }
  183. // return null;
  184. //}
  185. //Process p = new Process();
  186. //p.StartInfo.UseShellExecute = false;
  187. ////AppDomain.CurrentDomain.BaseDirectory Application.StartupPath 两种获取本地地址方式
  188. //p.StartInfo.FileName = Application.StartupPath + "\\US.Browser.IE.Update.exe";
  189. //p.StartInfo.CreateNoWindow = true;
  190. //p.Start();
  191. // //Environment.Exit(0);
  192. // // Application.Exit(); 居然还会继续执行下面的内容
  193. //using (Mutex _UM = new Mutex(true, Application.ProductName, out _UTF))
  194. //{
  195. // //MessageBox.Show(Environment.CommandLine);
  196. // //Console.WriteLine("CommandLine: {0}", Environment.CommandLine);
  197. // if (_UTF)
  198. // {
  199. // Application.SetCompatibleTextRenderingDefault(false);//美观等考虑,以及多语言字体的兼容问题
  200. // //string[] arg = Environment.GetCommandLineArgs();//获取命令 第一个为文件名
  201. // //Update.Update UD = new Update.Update();//加载下载页面
  202. // //try
  203. // //{
  204. // // if (UD.CFUTF()) { Application.Run(UpdateCue.NewUpCF()); return; }//启动下载更新
  205. // //}
  206. // //catch (Exception e) { }
  207. // Application.Run(StaticClass.getI(Environment.CommandLine));//启动应用程序
  208. // }
  209. //}
  210. #endregion
  211. }
  212. }