在 Program.cs 打开的时候在里面修改
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (IsRunning())
{
MessageBox.Show("程序已经打开!");
return;
}
Application.Run(new Main());
}
public static bool IsRunning()
{
Process current = default(Process);
current = System.Diagnostics.Process.GetCurrentProcess();
Process[] processes = null;
processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
Process process = default(Process);
foreach (Process tempLoopVar_process in processes)
{
process = tempLoopVar_process;
if (process.Id != current.Id)
{
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return true;
}
}
}
return false;
}
本文介绍了一种使用 C# 实现的方法来防止 Windows 应用程序启动多个实例。通过检查当前进程与已运行进程的名称及路径是否相同来判断是否已有实例正在运行。

6597

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



