C# 注册自定义协议头 浏览器唤醒程序exe。

        /// <summary>
        /// 注册协议头项到注册表
        /// </summary>
        public static void RegURLProtocol()
        {
            try
            {
                //注册的协议头,即在地址栏中的路径 如cyrmt://xxxxx/xxx
                var surekamKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("cyrmt");
                //以下这些参数都是固定的,不需要更改,直接复制过去 
                var shellKey = surekamKey.CreateSubKey("shell");
                var openKey = shellKey.CreateSubKey("open");
                var commandKey = openKey.CreateSubKey("command");
                surekamKey.SetValue("URL Protocol", "");
                //这里可执行文件取当前程序全路径,可根据需要修改
                string exePath = Process.GetCurrentProcess().MainModule.FileName;
                commandKey.SetValue("", "\"" + exePath + "\"" + " \"%1\"");
            }
            catch { }
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        public static void UnURLProtocol()
        {
            try
            {
                //直接删除节点
                Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree("cyrmt");
            }
            catch { }
        }

  

posted @ 2023-08-16 15:19  懒人境界  阅读(69)  评论(0编辑  收藏  举报