最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
确保只有一个程序实例运行(C#)之解决方案
时间:2022-07-02 11:34:26 编辑:袖梨 来源:一聚教程网
转帖自/20030407/1661941.shtml
如何确保在C#中只有一个程序(实例)运行?
解答:主要应用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;
}
如何确保在C#中只有一个程序(实例)运行?
解答:主要应用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;
}
相关文章
- 三角洲行动改装方案大全 全枪械最新热门改枪代码合集 09-15
- 魔兽世界洛克莫丹稀有宝宝在哪抓-洛克莫丹稀有宝宝位置分享 09-15
- 无主之地4锻造骑士亚蒙技能树一览 锻造骑士亚蒙职业技能有什么 09-15
- 无主之地4铁骨战士法拉技能树一览 铁骨战士法拉职业技能有什么 09-15
- 三角洲行动步枪怎么改 步枪热门改枪方案整理分享 09-15
- 无主之地4增加武器快捷栏位的方法 怎么增加武器快捷栏位 09-15