public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
return null;
}
然后在启动的位置写:
Process instance = RunningInstance();
if(instance == null)
{
Application.Run(new FrmMain());//启动窗体
}else
{
Application.Exit();
}
本文介绍了一种确保应用程序仅运行一个实例的方法。通过获取当前进程信息并与同名进程对比,判断是否存在重复运行的情况。若检测到重复实例,则退出当前进程,确保应用的单一运行。
435

被折叠的 条评论
为什么被折叠?



