Using system; using system.windows; using system.diagnostics; using system.reflection;
public class OneInstnace {[STAThread] public static void Main () {// Get the running instance Process instance = RunningInstance ();.. if (instance == null) {// There is not another instance, show our form Application .Run (new Form ());} else {// There is another instance of this process HandleRunningInstance (instance);.}} 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 ("/", "//") == current.mainmodule.filename) {// Return The Other process instance. Return process;}}}
// no Other instance. Return null. 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 setForegroundWindow (instance.mainwindowhandle);
[DLLIMPORT ("User32.dll")]]]]]
private static extern bool ShowWindowAsync (IntPtr hWnd, int cmdShow); [DllImport ( "User32.dll")] private static extern bool SetForegroundWindow (IntPtr hWnd); private const int WS_SHOWNORMAL = 1;}