<p>In a C# Windows Forms application I want to detect if another instance of the application is already running.
If so, activate the main form of the running instance and exit this instance.</p>

<p>What is the best way to achieve this?</p>

[{'answer_id': 51902, 'author': 'Dean Hill', 'author_id': 3106, 'author_profile': 'https://Stackoverflow.com/users/3106', 'pm_score': 3, 'selected': False, 'text': '<p>Here is what I\'m currently doing in the application\'s Program.cs file.</p>\n\n<pre><code>// Sets the window to be foreground\n[DllImport("User32")]\nprivate static extern int SetForegroundWindow(IntPtr hwnd);\n\n// Activate or minimize a window\n[DllImportAttribute("User32.DLL")]\nprivate static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);\nprivate const int SW_RESTORE = 9;\n\nstatic void Main()\n{\n    try\n    {\n        // If another instance is already running, activate it and exit\n        Process currentProc = Process.GetCurrentProcess();\n        foreach (Process proc in Process.GetProcessesByName(currentProc.ProcessName))\n        {\n            if (proc.Id != currentProc.Id)\n            {\n                ShowWindow(proc.MainWindowHandle, SW_RESTORE);\n                SetForegroundWindow(proc.MainWindowHandle);\n                return;   // Exit application\n            }\n        }\n\n\n        Application.EnableVisualStyles();\n        Application.SetCompatibleTextRenderingDefault(false);\n        Application.Run(new MainForm());\n    }\n    catch (Exception ex)\n    {\n    }\n}\n</code></pre>\n'}, {'answer_id': 51907, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 4, 'selected': True, 'text': '<p>Scott Hanselman <a href="http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx" rel="noreferrer">answers</a> on you question in details.</p>\n'}, {'answer_id': 51921, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>Aku, that is a good resource.  I answered a question similar to this one a while back.  You can check my <a href="https://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application#19326">answer here</a>.  Even though this was for WPF, you can use the same logic in WinForms.</p>\n'}, {'answer_id': 801707, 'author': '0x49D1', 'author_id': 47672, 'author_profile': 'https://Stackoverflow.com/users/47672', 'pm_score': 2, 'selected': False, 'text': '<p>You can use such detection and activate your instance after it:</p>\n\n<pre><code>        // Detect existing instances\n        string processName = Process.GetCurrentProcess().ProcessName;\n        Process[] instances = Process.GetProcessesByName(processName);\n        if (instances.Length &gt; 1)\n        {\n            MessageBox.Show("Only one running instance of application is allowed");\n            Process.GetCurrentProcess().Kill();\n            return;\n        }\n        // End of detection\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51898', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3106/']