using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; namespace WindowsFormsApplication2 { static class Program { [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")] public static extern int SetForegroundWindow(IntPtr hwnd); /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); //防止程序启动多次,只有一个运行的实例 if (p.Length > 1) { //将之前启动的窗体显示出来。 SetForegroundWindow(p[1].MainWindowHandle); return;//退出软件 } //正常情况下的窗体显示 Application.Run(new Form1()); } } } 建议使用API
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices; namespace WindowsFormsApplication2
{
static class Program
{
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern int SetForegroundWindow(IntPtr hwnd); /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
//防止程序启动多次,只有一个运行的实例
if (p.Length > 1)
{
//将之前启动的窗体显示出来。
SetForegroundWindow(p[1].MainWindowHandle);
return;//退出软件
}
//正常情况下的窗体显示
Application.Run(new Form1());
}
}
}
建议使用API