//判断是否已经存在一个exe
是要写在main函数里面的
[STAThread]
static void Main() 
{
bool createdNew;
Mutex m = new Mutex(true, "yourexe", out createdNew);
if (! createdNew)
{
MessageBox.Show("Only one exe is allowed at a time.");
return;
}
Application.Run(new Start());
GC.KeepAlive(m);
}上面的要添加using System.Threading;
Mutex class是用于进程同步的

解决方案 »

  1.   

    //check most process
    Process[] processes = Process.GetProcessesByName(yourProcessesName);
    if (processes.Length >= 2 )    
    {
        //处理多次运行
      ......
      return;
    }
      

  2.   

    [C#] 
    public static Process RunningInstance() 

         Process current = Process.GetCurrentProcess(); 
         Process[] processes = Process.GetProcessesByName (current.ProcessName);       //Loop through the running processes in with the same name 
          foreach (Process process in processes) 
          { 
              //Ignore the current process 
               if (process.Id != current.Id) 
              { 
                   //Make sure that the process is running from the exe file. 
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) 
                    { 
                        //Return the other process instance. 
                        return process; 
                    } 
               } 
          } 
          //No other instance was found, return null. 
          return null; 
    } [VB.NET] 
    Public Shared Function RunningInstance() As Process 
         Dim current As Process = Process.GetCurrentProcess() 
         Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)      'Loop through the running processes in with the same name 
         Dim process As Process 
         For Each process In processes 
              'Ignore the current process 
              If process.Id <> current.Id Then 
                   'Make sure that the process is running from the exe file. 
                   If [Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then 
                        'Return the other process instance. 
                        Return process 
                   End If 
              End If 
         Next process 
         'No other instance was found, return null. 
         Return Nothing 
    End Function 'RunningInstance 
      

  3.   

    贴个例子:
    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Reflection; public class OneInstnace
     { 
     [STAThread]
     public static void Main()
     {
     //Get the running instance.
     Process instance = RunningInstance();
     if (instance == null)
     {
     //There isn't another instance, show our form.
     Application.Run (new Form());
     }
     else
     {
     //There is another instance of this process.
     HandleRunningInstance(instance);
     }
     }
     public static Process RunningInstance()
     {
     Process current = Process.GetCurrentProcess();
     Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name
     foreach (Process process in processes)
     {
     //Ignore the current process
     if (process.Id != current.Id)
     {
     //Make sure that the process is running from the exe file.
     if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
     current.MainModule.FileName)
     {
     //Return the other process instance.
     return process;
     }
     }
     } //No other instance was found, return null.
     return null;
     }
     public static void HandleRunningInstance(Process instance)
     {
     //Make sure the window is not minimized or maximized
     ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to foreground window
     SetForegroundWindow (instance.MainWindowHandle);
     } [DllImport("User32.dll")] private static extern bool ShowWindowAsync(
     IntPtr hWnd, int cmdShow);
     [DllImport("User32.dll")] private static extern bool
     SetForegroundWindow(IntPtr hWnd);
     private const int WS_SHOWNORMAL = 1;
     } 
      

  4.   

    用API 函数FINDWINDOW、SHOWWINDOW可以办到,停简单的
      

  5.   

    using System.Diagnostics; /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    // 防止多次执行
    Process[] processes = Process.GetProcessesByName("RCL");
    if (processes.Length >= 2 )    
    {
    MessageBox.Show("程序已经执行!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
    return;
    }
    else
    Application.Run(new frmMain());
    }
      

  6.   

    官方代码
    VB.NET:if (UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0)
    {
        MessageBox.Show("already started");
    }