怎么判断本程序在系统进程里只有一个,如果已经有了就不启动?

解决方案 »

  1.   

    using System;
    using System.Diagnostics;
    using System.Reflection;
    ...
    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; 
     
    }
      

  2.   

    static void Main() 
    {
    Process proce = myClass.RunningInstance();
    if(proce == null)
    {
       Application.Run(new Form1());
    }
    }
      

  3.   

    http://msdn.microsoft.com/library/en-us/vbcon/html/vbconprevinstancepropertychangesinvisualbasicnet.aspPrevInstance Property Changes in Visual Basic .NET
    In Visual Basic 6.0, the PrevInstance property of the App object was used to determine if a previous instance of an application was running. There is no equivalent for this property in Visual Basic .NET; however, the following code can be used to test for a previous instance:' Visual Basic .NET
    Function PrevInstance() As Boolean
       If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
          Return True
       Else
          Return False
       End If
    End Function
    Note   The behavior is slightly different than that of Visual Basic 6.0. In Visual Basic 6.0, PrevInstance returned true only if the full path and file name were exactly the same; in Visual Basic .NET, this function will return true for two instances started from different paths. In addition, PrevInstance would never return true for the first instance of an application; in Visual Basic .NET, once a second instance is loaded, the first instance will also return true.
    See Also
    App Object Changes in Visual Basic .NET 
      

  4.   

    to use it:
        Public Sub Main()
           If PrevInstance() Then Exit Sub       ' continue with your application
           UserName = Environ("UserName")
           ComputerName = Environ("COMPUTERNAME")     End Sub
      

  5.   

    用互斥体,需要加using 引用,具体的命名空间忘了,自己查一下吧          [STAThread]
    static void Main() 
    {
    //声明互斥体。
    Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
    //判断互斥体是否使用中。
    bool Running = !mutex.WaitOne(0, false);
    if (! Running)
    Application.Run(new Form1());
    else
    MessageBox.Show("应用程序已经启动!"); //Application.EnableVisualStyles();
    //Application.Run(new Form1());//窗体名称
    }
      

  6.   

    下面网站中提供了两种简单的方法,一定可以为你解决问题!http://xt0055.27h.com/csharp-01-02.htm
      

  7.   

    System.Threading.Mutex mutex = new System.Threading.Mutex(false, "ThisShouldOnlyRunOnce");
      bool Running = !mutex.WaitOne(0, false);
      if (! Running)
        Application.Run(new FormMain());
      else
        MessageBox.Show("應用程序已啟動!");
      

  8.   

    false, "ThisShouldOnlyRunOnce");
      bool Running = !mutex.WaitOne(0, false);
      if (! Running)
        Application.Run(new FormMain());
      else
        MessageBox.Show("應用程序已啟動!");