[C#] C#确保只有一个实例在运行 →→→→→进入此内容的聊天室

来自 , 2020-02-02, 写在 C#, 查看 154 次.
URL http://www.code666.cn/view/bf499a12
  1. public static Process RunningInstance()
  2. {
  3. Process current = Process.GetCurrentProcess();
  4. Process[] processes = Process.GetProcessesByName (current.ProcessName);
  5. //查找相同名称的进程
  6. foreach (Process process in processes)
  7. {
  8. //忽略当前进程
  9. if (process.Id != current.Id)
  10. {
  11. //确认相同进程的程序运行位置是否一样.
  12. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  13. {
  14. //Return the other process instance.
  15. return process;
  16. }
  17. }
  18. }
  19. //No other instance was found, return null.
  20. return null;
  21. }
  22. [VB.NET]
  23. //csharp/5597

回复 "C#确保只有一个实例在运行"

这儿你可以回复上面这条便签

captcha