本文实例讲述了C#确保只有一个实例在运行的方法。分享给大家供大家参考。具体实现方法如下:
public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //查找相同名称的进程 foreach (Process process in processes) { //忽略当前进程 if (process.Id != current.Id) { //确认相同进程的程序运行位置是否一样. if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } [VB.NET]
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答