public static bool AlreadyRunning()
{
Process current = Process.GetCurrentProcess(); 
foreach(Process process in Process.GetProcessesByName(current.ProcessName)) 
{
if (process.Id != current.Id) 

if(process.MainModule.FileName == current.MainModule.FileName)
{
return true;



return false;
}

解决方案 »

  1.   

    如何让应用程序只有一个实例在运行?
    http://dotnet.aspx.cc/ShowDetail.aspx?id=E2A17727-765F-4346-8446-5D130622CB54
      

  2.   

    System.Diagnostics.Process[] pro= System.Diagnostics.Process.GetProcessesByName(ds.Tables[0].Rows[i]["appname"].ToString());
    try
    {
    int pid=pro[0].Id;
    //MessageBox.Show(i.ToString());
    }
    catch
    {
    Directory.SetCurrentDirectory(ds.Tables[0].Rows[i]["dirpath"].ToString());
    System.Diagnostics.Process.Start(ds.Tables[0].Rows[i]["apppath"].ToString());
    this.listBox1.Items.Add("["+DateTime.Now.ToString()+"]重启:"+ds.Tables[0].Rows[i]["appname"].ToString());
    }
      

  3.   

    using System.Threading;  static void Main() 
      {
       Mutex stMutex;
       bool bCreatedNew = false;
       stMutex = new Mutex(true,"MyOneProcMutex",out bCreatedNew);
       if(bCreatedNew == true)
       {
        //Do your work here.   }
       else
       {
        MessageBox.Show("Another instance is running, I will quit.");
       }
       stMutex.Close();
      }