判断一下进程,再调一下API,将已经运行的进程前置就可以了
例如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace _415_Calculator
{
static class Program
{
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd); ///
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process proc = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(proc.ProcessName);
if (processes.Length > 1)
{
MessageBox.Show("程序已经运行");
foreach (Process pro in processes)
{
SetForegroundWindow(pro.MainWindowHandle);
}
}
else
{
Application.Run(new Form1());
}
}
}
}