How to check if another instance of the application is running?

Sometimes we need to check the application instance running status.

var isExists = System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1;

If you want to kill the currently loading process instantly application process, you can use below code.

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();

You need to add a reference to System.Core.dll for the .Count() extension method. Alternatively, you can use the .Length property.

Leave a Reply