Total instances running of application in C# .NET

August 3, 2010 by: Sanket

Total number of instances running of application in C# .NET

Many times we needs to find how many instances of particular application or process are running.

This can be used in allowing to run only single instance of application or at most 2 etc.

Also count can be useful for licensing and other stuff.

So here is simple code to find out how many number of instances are running for particular application or process.

    string fileName = Process.GetCurrentProcess().MainModule.FileName;
    int count = 0;
    foreach (Process p in Process.GetProcesses())
    {
        try
         {
            if (p.MainModule.FileName == fileName)
            {
                count++;
            }
        }
        catch { }
    }

    MessageBox.Show("Total Instances Running are " + count);

If you want to do anything additional to this and not aware how to do then please post comment and wait for reply !!!

Leave a Reply

*