Program.cs 1.8 KB

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