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);
            }
          }

        }

转载于:https://www.cnblogs.com/starxp/articles/1828367.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值