1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| [DllImportAttribute("User32.dll")] private static extern int FindWindow(string ClassName, string WindowName);
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(int hWnd);
private void checkProcess() { Console.WriteLine("程序启动"); Process current = Process.GetCurrentProcess(); string strProcessName = Process.GetCurrentProcess().ProcessName; var processList = Process.GetProcessesByName(strProcessName); if (processList.Length > 1) { foreach (var process in processList) { if (process.Id == current.Id) continue; int hWnd = FindWindow(null, process.MainWindowTitle.ToString()); SetForegroundWindow(hWnd); } Environment.Exit(1); return; } }
|