只允许运行一个C#程序

转自:<a target=_blank href="http://www.cnblogs.com/marssl/archive/2009/01/27/1381032.html">http://www.cnblogs.com/marssl/archive/2009/01/27/1381032.html</a>
 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool blnIsRunning;
            Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out blnIsRunning);
            if (!blnIsRunning)
            {
                MessageBox.Show("程序已经运行!", "提示",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }   
            RegIPWorks();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        #region 注册IPWorks
        /// <summary>
        /// 注册IPWorks
        /// </summary>
        private  static void RegIPWorks()
        {
            try
            {
                string keyPath = "SOFTWARE\\nsoftware\\RT\\IPN8A";
                RegistryKey regKey = Registry.LocalMachine.OpenSubKey(keyPath);
                if (regKey == null)
                {
                    regKey = Registry.LocalMachine.CreateSubKey(keyPath);
                    regKey.SetValue("", "IPN8AB-87892657-WGP-WRFDM");
                    regKey.SetValue("*", "B2YA-M89W-TM5J");
                }
                regKey.Close();
            }
            catch
            {
                CommFun.WriteSysLog("注册IPWorks产生错误。");
            }
        }
        #endregion 注册IPWorks

    }

<pre name="code" class="csharp">              //=====创建互斥体法:=====
            //bool blnIsRunning;
            //Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out   blnIsRunning);
            //if (!blnIsRunning)
            //{
            //    MessageBox.Show("程序已经运行!", "提示",
            //    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //    return;
            //}   

 

 

 

                        //保证同时只有一个客户端在运行   
            //System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false, "OnePorcess.exe");
            //if (!mutexMyapplication.WaitOne(100, false))
            //{
            //    MessageBox.Show("程序" + Application.ProductName + "已经运行!", Application.ProductName,
            //    MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

 

 

//=====判断进程法:(修改程序名字后依然能执行)=====
            //Process current = Process.GetCurrentProcess();
            //Process[] processes = Process.GetProcessesByName(current.ProcessName);
            //foreach (Process process in processes)
            //{
            //    if (process.Id != current.Id)
            //    {
            //        if (process.MainModule.FileName
            //        == current.MainModule.FileName)
            //        {
            //            MessageBox.Show("程序已经运行!", Application.ProductName,
            //            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //            return;
            //        }
            //    }
            //}     


 http://www.cnblogs.com/marssl/archive/2009/01/27/1381032.html 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值