如何检查我的 C# Windows 应用程序是否正在运行?
我知道我可以检查进程名称,但如果 exe 更改,则名称可以更改。
有什么方法可以让我的应用程序具有哈希键或其他东西吗?
最佳答案
public partial class App : System.Windows.Application
{
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
protected override void OnStartup(StartupEventArgs e)
{
// Get Reference to the current Process
Process thisProc = Process.GetCurrentProcess();
if (IsProcessOpen("name of application.exe") == false)
{
//System.Windows.MessageBox.Show("Application not open!");
//System.Windows.Application.Current.Shutdown();
}
else
{
// Check how many total processes have the same name as the current one
if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
{
// If ther is more than one, than it is already running.
System.Windows.MessageBox.Show("Application is already running.");
System.Windows.Application.Current.Shutdown();
return;
}
base.OnStartup(e);
}
}
关于c# - 检查我的 Windows 应用程序是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4722198/