在你的程序启动的代码中加入如下的的代码就可以了:
System.Diagnostics.Process[] pros=System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if (pros.Length>1)
{
Application.Exit();
return;
}

解决方案 »

  1.   

    不过为了保险起见我一般这样用:
    static void Main() 
    {
    try
    {
    RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\PerfProc\Performance", true);
    if (rk!=null)
    {
    object tmpObj=rk.GetValue("Disable Performance Counters");
    if (tmpObj!=null && !tmpObj.Equals(0))
    {
    rk.SetValue("Disable Performance Counters",0);
    //要添加一个日志表明此处删除了一个注册表值
    }
    }
    System.Diagnostics.Process[] pros=System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
    if (pros.Length>1)
    {
    Application.Exit();
    return;
    }
    Application.Run(new frmMainServer());
    }
    catch (Exception ex)
    {
    System.Console.WriteLine(ex.Message);
    }
    }
      

  2.   

    在Main()里面加bool createdNew;
    Mutex m=new Mutex(true,"test",out createdNew);
    if (createdNew)
    {
    Application.Run(new Form());
    m.ReleaseMutex();
    }
    else
    {
    MessageBox.Show("本程序只允许同时运行一个");
    }
      

  3.   

    http://www.codeproject.com/cpp/avoidmultinstance.asp
    尽点力
      

  4.   

    Copy的别人的代码,忘了是谁的啦。:)
    下面是肯定可以的:方法:改写程序启动的函数(main())。
    具体方法:
    [STAThread]
    static void Main() 
    {
                int ProceedingCount = 0;
                Process[] ProceddingCon = Process.GetProcesses();
                foreach(Process IsProcedding in ProceddingCon)
                {
                    if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        ProceedingCount += 1;
                    }
                }
                if(ProceedingCount > 1)
                {
                    MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.DoEvents();
                    Application.Run(new [本系统的主界面]());
                }
    }
      

  5.   

    [STAThread] 
          static void Main() { 
             Process[] myProcesses = Process.GetProcessesByName(Application.ProductName  ); 
             if(myProcesses.Length<=1) 
                Application.Run(new LogOn()); 
             else 
                MessageBox.Show("程序已经启动!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); 
                Application.Exit(); 
          } 
      

  6.   

    to hbxtlhx(下着春雨的天), roking(洛克), zmgcj(真的名贵)  : 
    精简版C#不支持 System.Diagnostics.Processto weilysunhg(一天到晚红烧的鱼) :
    精简版C#不支持 Mutex m=new Mutex(true,"test",out createdNew);
    Mutex仅仅支持 new Mutex() 和 new Mutex(bool);to hbxtlhx(下着春雨的天):
    精简版C#不支持 RegistryKey.
      

  7.   

    如果不支持就不好说了,假如连API也不能用,那么这个工具我们还用他干嘛,所以,不要用那个啦,换个完整版的吧.多好.但是这个好像有了framework就应有了这个命名空间了,就可以用了,如果真的用全整版写了程序却不能在精简版运行了,这不是成了大问题了.
      

  8.   

    建议换装一个全整.NET framework,这个可是免费的啊,也不算太大.
      

  9.   

    to hbxtlhx(下着春雨的天):
    老兄,Pocket PC 2003是微软提供的PDA的一种基于Windows CE.net的操作系统,里面的ROM内置了 .net framework精简版,不能使用完整版。不是我不想用完整版,而是环境不允许,受到太多的限制。
      

  10.   

    看来和你的环境有关了,Pocket PC 2003不懂,不说话了,看着大家怎么解决问题吧,呵呵......
      

  11.   

    找了一个传统方法,看下面代码:///Class FileMutex
    public class FileMutex
    {
       private FileStream aFile;
       public FileMutex()
       {
       }
       public bool Open()
       {
          string strAppDir=Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
          string strFile=strAppDir+@"\file.dat";      try
          {
             aFile=new FileStream(strFile, FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None);
          }
          catch
          {
             return false;
          }      return true;
       }
       public void Dispose()
       {
          try
          {
             aFile.Close();
             aFile=null;
          }
          catch
          {
          }
       }
    }//使用类 FileMutex
    static void Main() 
    {
       FileMutex f=new FileMutex();
       if(f.Open())
          Application.Run(new MainFrm());
       f.Dispose();
    }可以有效解决问题。