c# – 检查我的Windows应用程序是否正在运行

如何检查我的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);
        }
    }
http://stackoverflow.com/questions/4722198/checking-if-my-windows-application-is-running

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c# – 检查我的Windows应用程序是否正在运行