C# 避免程序重复启动(二次启动)

static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] Args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            bool ret;
            Mutex mutex = new Mutex(true, Application.ProductName, out   ret);
            if (ret)
            {
                //如果没有参数,则退出.
                if (Args[0] == null || Args[0] == "")
                {
                    MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //如果参数不是以fpcctvgis开头,则非法:
                if (!Args[0].Contains("fpcctvgis://"))
                {
                    MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //MessageBox.Show(Args[0]);
                //分离出来后面加密的字符串
                string str = Args[0].Replace("fpcctvgis://", "");
                //分离出最后一个 "/" 符号  传递格式为
                //fpcctvgis://vEhSyFPOBZ4fckjX4nYQFx2gCT1k2aKn/
                str = str.Substring(0, str.Length - 1);
                //MessageBox.Show(str);
                //对参数进行解码,如果解码失败,则退出.
                try
                {
                    str = PermissionHelper.Decrypt(str);
                }
                catch
                {
                    MessageBox.Show("参数错误.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //分离出来三个参数
                string[] parms = str.Split(' ');
                //长度应该为3个
                if (parms.Length == 3)
                {
                    if (parms[0] != "" && parms[1] != "" && parms[2] == "futureproduct")
                    {
                        //MessageBox.Show(parms[0] + "\n" + parms[1] + "\n" + parms[2]);
                        try
                        {
                            //if (Args[0].Contains("fpcctvgis:"))
                            //{
                            //    Args[0] = Args[0].Replace("fpcctvgis:", "");
                            userpermision.userflag = parms[1].Trim();
                            userpermision.userid = parms[0].Trim();
                            //如果登录用户为家长.需要加密狗.
                            if (parms[1].Trim() == "2")
                            {
                                byte[] dogID = DOGFunc.ReadDog((uint)100, (uint)0);
                                if (dogID == null || dogID.Length == 0)
                                {
                                    MessageBox.Show("请插入光盘配套的加密狗以运行本程序!", "菲博特电子科技(深圳)有限公司", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    return;
                                }
                            }
                            Application.Run(new frmDefault());

                           mutex.ReleaseMutex();

                            //}
                            //else
                            //{
                            //    MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                            //        MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //    return;
                            //}
                        }
                        catch
                        {
                            MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                    }
                    else
                    {
                        MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("请先启动主程序.\n有疑问请联系菲博特电子科技(深圳)有限公司", "提示信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


                //测试专用
                //userpermision.userflag = "2";//权限标识,0为管理员(园长等),1为老师,2为家长
                ////userpermision.userid = "2";
                //userpermision.userid = "1";
                //Application.Run(new frmDefault());
            }
            else
            {
                MessageBox.Show("该程序已经启动", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
          }

        }

posted on 2010-09-16 19:52  peter_zhang  阅读(3481)  评论(0编辑  收藏  举报