1、需要引用using System.Runtime.InteropServices;

2、

[DllImport(“user32.dll”, CharSet = CharSet.Auto)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
[DllImport(“user32.dll”, CharSet = CharSet.Auto)]
public static extern void SetForegroundWindow(IntPtr hWnd);

3、

static void KillCurrentAndShowMutexThread()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (process.MainModule.FileName == current.MainModule.FileName)
{
//Console.WriteLine(“Another same process is loading.”);
//Console.WriteLine(“Current window will close in 3 seconds, current Id ={0}.”, current.Id);
//Thread.Sleep(3000);
IntPtr handle = process.MainWindowHandle;
//SetForegroundWindow和SwitchToThisWindow都能实现程序置于最前
//SetForegroundWindow不能防已被最小化的程序
//SetForegroundWindow(handle);
//SwitchToThisWindow即使第一个程序被最小化也可以显示到最前
SwitchToThisWindow(handle, true);
//用kill杀死刚开启的进程;
current.Kill();
}
}
}
return;
}