I was working on some code that would provide a means to quickly tell if the application that you are starting is actually already running, actually it was for the CommonRSS application that I am working. I have done similar things in Visual Basic 6 but I do not remember ever trying this in C#. I am wondering if there is a much cleaner way of doing this ... any thoughts?

using System.Diagnostics;

private const string APPLICATION_NAME = "CommonRSS";

public static bool IsAppAlreadyRunning
{
    get
    {
        bool isAlreadyRunning = false;
        Process currentProcess = Process.GetCurrentProcess();
        Process[] processes = Process.GetProcesses();
        foreach (Process process in processes)
        {
            if (currentProcess.Id != process.Id)
            {
                if (APPLICATION_NAME == process.ProcessName)
                {
                    isAlreadyRunning = true;
                    break;
                }
            }
        }
        return isAlreadyRunning;
    }
}

Technorati Tags: