请教给位高手,在c#中如何防止一个程序被同时启动多次(不用FindWindow方法),
另外,为什么我调用GetLastError(),返回127(在delphi中返回0)。
请高人指点一下,万分感谢!!!

解决方案 »

  1.   

    http://blog.csdn.net/eagleeye/archive/2006/06/27/840155.aspx
    看看这里,我也是收集的
      

  2.   

    也可以用一个标志来控制,比如写个ini文件,判断一下值,比如1就是打开了,0就是没有打开
      

  3.   

    To zhangci226(三只熊熊)
    但是如果程序意外终止,ini来不及改回去,那下次不就开不了了?
      

  4.   

    使用Mutex方法是比较经典的,采用ini文件或者注册表进行记录是不合理的,按照楼上的说法,软件退出时,就应该将ini文件的配置置为0,但碰上死机、程序异常退出时怎么办?重新启动软件时,会显示永远有一个实例在运行,除非手工将ini文件的设置改为0,但用户怎么会改呢?
      

  5.   

    从系统进程中获得信息process类
    获得windows打开的所有进程,然后比较进程明,看有没有你的程序的进程,以及进程数量
    这样就可以控制打开数量了
      

  6.   

    msdn的例子using System;
    using System.Diagnostics;
    using System.ComponentModel;namespace MyProcessSample
    {
        /// <summary>
        /// Shell for the sample.
        /// </summary>
        public class MyProcess
        {
            
           
            
            public void BindToRunningProcesses()
            {
                // Get the current process.
                Process currentProcess = Process.GetCurrentProcess();            
                // Get all instances of Notepad running on the local
                // computer.
                Process [] localByName = Process.GetProcessesByName("notepad");            
                // Get all instances of Notepad running on the specifiec
                // computer.
                // 1. Using the computer alias (do not precede with "\\").
                Process [] remoteByName = Process.GetProcessesByName("notepad", "myComputer");
                
                // 2. Using an IP address to specify the machineName parameter. 
                Process [] ipByName = Process.GetProcessesByName("notepad", "169.0.0.0");
                
                
                // Get all processes running on the local computer.
                Process [] localAll = Process.GetProcesses();            
                // Get all processes running on the remote computer.
                Process [] remoteAll = Process.GetProcesses("myComputer");            
                // Get a process on the local computer, using the process id.
                Process localById = Process.GetProcessById(1234);            
                // Get a process on a remote computer, using the process id.
                Process remoteById = Process.GetProcessById(2345, "myComputer");
                
            }
            
            public static void Main()
            {
                    
                       MyProcess myProcess = new MyProcess();
                            myProcess.BindToRunningProcesses();            }    
        }
    }