Program.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace ShareWifi
  9. {
  10. static class Program
  11. {
  12. /// <summary>
  13. /// 全局窗体List,便于相互跳转
  14. /// </summary>
  15. public static List<Form> formList = new List<Form>();
  16. /// <summary>
  17. /// The main entry point for the application.
  18. /// </summary>
  19. [STAThread]
  20. static void Main()
  21. {
  22. Process instance = RunningInstance();
  23. if (instance == null)
  24. {
  25. Application.EnableVisualStyles();
  26. Application.SetCompatibleTextRenderingDefault(false);
  27. Application.Run(new MainForm());
  28. }
  29. else
  30. {
  31. // MessageBox.Show("程序已经在运行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  32. }
  33. }
  34. public static Process RunningInstance()
  35. {
  36. // 获取当前活动的进程
  37. Process current = Process.GetCurrentProcess();
  38. // 获取当前本地计算机上指定的进程名称的所有进程
  39. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  40. foreach (Process process in processes)
  41. {
  42. // 忽略当前进程
  43. if (process.Id != current.Id)
  44. {
  45. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  46. {
  47. return process;
  48. }
  49. }
  50. }
  51. // 如果没有其他同名进程存在,则返回 null
  52. return null;
  53. }
  54. }
  55. }