上文是在网上找的检测程序重复运行的类,可是感受不是很好用,并且还使用了API,彷佛彻底没有必要,因而晚上本身写了一个函数,通过测试,在多用户下仍然能够检测到程序的屡次运行。固然,若是程序改了名字仍是能够再次运行,不过这种方式只怕没有什么太好的办法来,除非是在.NET环境或注册表中写入一些标志,但彷佛也没有必要。函数
if (AppInstance()) { MessageBox.Show("警告:程序正在运行中! 请不要重复打开程序!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } public static bool AppInstance() { Process[] MyProcesses = Process.GetProcesses(); int i = 0; foreach (Process MyProcess in MyProcesses) { if (MyProcess.ProcessName == Process.GetCurrentProcess().ProcessName) { i++; } } return (i > 1) ? true : false; }
其实就在是进程中检查进程名,若是多于一个,则返回TRUE测试
出处:http://blog.csdn.net/lanman/article/details/3655972spa