通过网页打开winform程序

winform页面代码:

        /// <summary>
        /// 注册表的写入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiWriteRegister_Click(object sender, EventArgs e)
        {
            try
            {
                GetRegistData("FysyDataCenter");
                MessageBox.Show("注册成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        /// <summary>
        /// 写入注册表以使用网页唤醒程序
        /// </summary>
        public static void GetRegistData(string name)
        {
            /**搜索到注册表根目录**/
            RegistryKey hkml = Registry.ClassesRoot;
            /**搜索到注册表根目录下的name文件夹**/
            RegistryKey software = hkml.OpenSubKey(name, true);
            if (software == null)
            {
                /**注册表未注册,添加注册表**/
                Enroll(name);
            }
        }

        /// <summary>
        /// 写入注册表
        /// </summary>
        public static void Enroll(string name)
        {
            /**获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。**/
            string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            RegistryKey regWrite = Registry.ClassesRoot.CreateSubKey(name);

            regWrite.SetValue(name.ToLower(), @"URL:" + str);
            regWrite.SetValue("URL Protocol", "URL Protocol");//不行可以删来看看
            regWrite.Close();

            regWrite = Registry.ClassesRoot.CreateSubKey(name + @"\shell");
            regWrite.Close();
            regWrite = Registry.ClassesRoot.CreateSubKey(name + @"\shell\open");
            regWrite.Close();
            regWrite = Registry.ClassesRoot.OpenSubKey(name + @"\shell\open", true);
            RegistryKey aimdir = regWrite.CreateSubKey("command");
            /**获取程序运行地址**/
            string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            string AppPath = Path.Combine(BaseDirectory, name + ".exe");

            aimdir.SetValue(@"", "\"" + str + "\" \" %1\"");
            regWrite.Close();
            aimdir.Close();
        }

可以在注册表中的: 计算机\HKEY_CLASSES_ROOT\FysyDataCenter看到;

注册完之后,你就可以用唤醒的名字去浏览器输入FysyDataCenter://,然后回车就出现一个要打开FysyDataCenter程序的弹窗了。

posted @ 2023-04-11 15:35  木子zzgxl  阅读(155)  评论(0编辑  收藏  举报