Thursday, January 7, 2010

Check Multiple Instances of an Application using C#

Simple code to check if multiple instances of current application are running in the machine using System.Diagnostics
        private static void check_application_process_instances()
        {
            Process[] oProcess;
            String sModuleName;
            String sProcessName;
            sModuleName = Process.GetCurrentProcess().MainModule.ModuleName;
            sProcessName = System.IO.Path.GetFileNameWithoutExtension(sModuleName);
            oProcess = Process.GetProcessesByName(sProcessName);
            if (oProcess.Length > 1)
            {
                MessageBox.Show("More than one instance is running!");
            }
        }