//start [DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("User32.dll")] private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); private const int WS_SHOWNORMAL = 1; private const int WM_COPYDATA = 0x004A; //end
public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) { //确保例程从EXE文件运行 if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //返回另一个例程实例 return process; } } } return null; }
public static void HandleRunningInstance(Process instance) { //确保窗口没有被最小化或最大化 ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);