C# Non-Restart Check If Application is Open

In the C # programming language, if our application is open, we may need to set a check to prevent it from reopening.

Below is the sample code for this control..

namespace ConsoleApplicationTest
{
    class Program {

        static void Main(string[] args) {

            Process prcess = Process.GetCurrentProcess();

            Process[] prcessList = Process.GetProcessesByName(prcess.ProcessName);

            if (prcessList.Length > 1)
            {
                Console.WriteLine("Application is Open");
                return;
            }

           
            Console.ReadLine();

        }
    }
}

In the example, we first find the process name of the application with our GetCurrentProcess method. Then we get all the processes running in the system according to this name and we collect it in your prcessList array variable.

If a process with this name is already running in the system, its number will be 2 with the current process. Since we only want 1 process to run, we give a warning if the number in our array variable meets the greater than 1 condition.



You May Interest

C# Finding the Path to the Windows Fonts Folder

Splitting a String By Desired Character in C#

Finding the Index Order of a Character in a String in C#

C# Finding the Operating System Username

C# Reverse Array