只允许运行一个C#程序

来源:互联网 发布:数据化人生 百度百科 编辑:程序博客网 时间:2024/05/16 15:21
转自:<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 0