Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
- System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess();
- foreach (System.Diagnostics.Process p in myProcesses)
- {
- if (p.ProcessName == me.ProcessName)
- if (p.Id != me.Id)
- {
- //if already running, abort this copy.
- return;
- }
- }
- //launch the application.
- //...
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- public static extern bool SetForegroundWindow(IntPtr hWnd);
- //...
- if (p.Id != me.Id)
- {
- //if already running, focus it, and then abort this copy.
- SetForegroundWindow(p.MainWindowHandle);
- return;
- }
- //...
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
- [STAThread]
- static void Main()
- {
- System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess();
- System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(me.ProcessName);
- foreach (System.Diagnostics.Process p in myProcesses)
- {
- if (p.Id != me.Id)
- {
- SwitchToThisWindow(p.MainWindowHandle, true);
- return;
- }
- }
- //now go ahead and start our application ;-)
- Process.GetProcessesByName(me.ProcessName)
- private const SW_SHOWNORMAL = 1
- <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
- Private Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As integer) As Boolean
- End Function
- <DllImport("user32.dll", SetLastError:=True)> _
- Private Function SetForegroundWindow(ByVal hwnd As IntPtr) As Boolean
- End Function
- Sub SetForeground()
- Dim processes As Process() = Process.GetProcessesByName("myprocess")
- For Each p as Process in processes
- ShowWindow(p.MainWindowHandle, SW_SHOWNORMAL)
- SetForegroundWindow(p.MainWindowHandle)
- Next
- End Sub
Add Comment
Please, Sign In to add comment