写了一个Console程序(可以改写为WindowsApplication),说一下我的实现:using System;
using System.Diagnostics;class OnlyOneInstance{
  public static void Main(){
    //public static Process[] GetProcesses();
    int iInstance = -1;
    Process[] processes = Process.GetProcesses();
    foreach(Process process in processes){
      string ProName = process.ProcessName;
      if(ProName == "OnlyOneInstance")
          iInstance++;
    }
    if(iInstance >= 1)
    {
     //结束它,使用原来的那个Process
    }
    else
    Console.ReadLine();//测试用
  }
}

解决方案 »

  1.   

    如果程序要在taskbar上show的话,可以使用FindWindow来查看
    using System;
    using System.Runtime.InteropServices;
    class OnlyOneInstance{
      [DllImport("user32.dll", EntryPoint="FindWindow")]
      public static extern int FindWindow (
    string lpClassName,
    string lpWindowName
      );
      
      public static void Main(){
       if(FindWindow(null,@"C:\WINDOWS\System32\cmd.exe - OnlyOneInstance") != 0)
           Console.WriteLine("Exists");
           Console.ReadLine();
        
      }
    }FindWindow(null,@"C:\WINDOWS\System32\cmd.exe - OnlyOneInstance") ;
    ---null 是类名,不清楚可以设置为null
    ---@"C:\WINDOWS\System32\cmd.exe - OnlyOneInstance" 是窗口的名字,若类名为空,这里必须设置精确
    ---返回值  0:未找到 otherwise找到
      

  2.   

    根据上边兄弟的意见,问题解决。 //定义Api方法FindWindow,该方法可以得到当在打开的某个窗口。
    [DllImport("user32.dll", EntryPoint="FindWindow")]
    public static extern int FindWindow(string lpClassName,string lpWindowName); //当某个窗口在最小化是将其进行显示。
    [DllImport("user32.dll", EntryPoint="ShowWindow")]
    public static extern int ShowWindow(int hwnd,int nCmdShow);
    //        //将窗口激活并且显示到顶部。
    //     [DllImport("user32.dll",EntryPoint="BringWindowToTop")]
    // public static extern int BringWindowToTop(int hwnd);
    //将被隐藏的窗口显示到前端
    [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
    public static extern int SetForegroundWindow(int hwnd);
      
    public static void Main()
    {
    int hWndHandler;//要查找的窗口句柄
    hWndHandler=FindWindow(null,"test");//运行的主窗口的名字是"test",当hWndHandler为0则没找到窗口,不为0意味着找到了窗口。
    if(hWndHandler!= 0)
    {

    ShowWindow(hWndHandler,9);//9--在窗口的显示状态中为正常显示窗口并激活之。
    SetForegroundWindow(hWndHandler);//将窗口显示到顶部
    }
    else
    {
    Application.Run(new frmTest());//正常状态下启动主程序。
    }
        
    }