public class OneInstnace
public static void Main()
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
//There isn't another instance, show our form.
Application.Run (new Form());
//There is another instance of this process.
public static Process RunningInstance()
Process current = Process.GetCurrentProcess();
Process processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
//Ignore the current process
if (process.Id != current.Id)
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
//Return the other process instance.
//No other instance was found, return null.
public static void HandleRunningInstance(Process instance)
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window