在C#中如何实现让一个.exe文件在本机上只能运行一次.

解决方案 »

  1.   

    以ini文件存储限制启动次数标志:
    OpenTims.ini:<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
    <a1:AppParams id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MyProject/MyProject%2C%20Version%3D1.0.2008.26559%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <OpenTimes id="ref-3">1</OpenTimes>
    </a1:AppParams>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    类AppParams读、写ini文件:public class AppParams
    {
    private const string sSERIALIZED_CLASS_FILENAME = "OpenTims.ini";
    private int OpenTims;
    private static string AppPath = System.Environment.CurrentDirectory.ToString(); public AppParams()
    {
    Initialize();
    } public void Initialize()
    {
    this.OpenTims= 0;
    } public void Store()
    {
    // serialize the current instance
    FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Create);
    SoapFormatter sfFormatter = new SoapFormatter();
    sfFormatter.Serialize(fsOutput,this);
    fsOutput.Close();
    } public static AppParams RetrieveSerialized()
    {
    try
    {
    AppParams apToReturn;

    if(File.Exists(sSERIALIZED_CLASS_FILENAME))
    {
    FileStream fsOutput = new FileStream(AppPath +"\\" +sSERIALIZED_CLASS_FILENAME, FileMode.Open);
    SoapFormatter sfFormatter = new SoapFormatter();
    apToReturn = (AppParams)sfFormatter.Deserialize(fsOutput);
    fsOutput.Close();
    }
    else
    {
    apToReturn = new AppParams();
    }
    return apToReturn;
    }
    catch(Exception ex)
    {
    throw new Exception(ex.Message);
    }
    } public int _opentims
    {
    get { return this.OpenTims;}
    set { this.OpenTims= value;}
    }主程序初始化判断:private void MyMainForm_Load(object sender, System.EventArgs e)
    {
                          AppParams app;
                          app = AppParams.RetrieveSerialized();
                          int opentimes = app._opentims;
                          if( opentimes == 1 )
                              this.Close();
                          else
                              app._opentims = 1;
            app.Store();
                       }
      

  2.   

    string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;if (procName != "你的exe的名称")
    {
    MessageBox.Show("工程名称被修改,不能启动!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    Application.Exit();
    return;
    }if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) > 0)
    {
    MessageBox.Show("该工程已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    Application.Exit();
    return;
    }
      

  3.   

    在.net中,有个Mutex类就是负责实现此功能的类,Mutex类是同步基元,它只向一个线程授予对共享资源的独占访问权。
    只要在加载的那个窗体里面把Main()方法稍微改一下就行了。
    static void Main() 
    {
    bool bExist;
    Mutex MyMutex=new Mutex(true,"OnlyRunOncetime",out bExist);
    if(bExist)
    {
    Application.Run(new Form1());
    MyMutex.ReleaseMutex();
    }
    else
    {
    MessageBox.Show("程序已经运行!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }

    }
      

  4.   

    而且用进程名字来限制的不是蛮好,也就是一个工程生成的EXE文件的名字完全被限定死了。如果别人要换个名字的话,那你又得改了
      

  5.   

    FindWindow函数互斥体System.Threading.Mutex
      

  6.   

    一般情况情况下都应该用 dugupiaoyun(独孤飘云) 提供的方法