控制软件只能运行一个实例

  要控制软件只运行一个实例,可以使用System.Diagnostics名字空间中的Process类来实现。

  思路:在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同,如是没有运行该程序,如果有,就将同名的同位置的程序窗口置前.

  代码实现
+展开
-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
}
来源:http://blog.csdn.net/ycl111/article/details/326575

加支付宝好友偷能量挖...


评论(0)网络
阅读(89)喜欢(0)Asp.Net/C#/WCF