Single instance of C# application

The System.Diagnostics namespace provides classes that allow you to interact with system processes, event logs, and performance counters. In order to determine if there are other instances of a C# application running in the system , we have to get a list of all the processes running in the system and check if an instance of the application is in the list or not.

using System.Diagnostics;

The Process class provides functionality to track system processes in a system and to start and stop system processes. Process.GetProcessesByName to create an array of new Process components and associate them with all the process resources that are running the same executable file on the local computer.

Process[] _process = null; _process = Process.GetProcessesByName (Process.GetCurrentProcess().ProcessName);

The above code _process array receiving all the associated processes. If the _process array contains more than one instance , then we should clear the instance already exist.

Full Source C#
using System; using System.Windows.Forms; using System.Diagnostics; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Process[] _process = null; _process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); if (_process.Length > 1) { MessageBox.Show("Multiple instancess running..."); } else { MessageBox.Show("Single instance only...."); } } } }