C# 将网页调用本地程序的URL Protocol 协议写到注册表中

来源:互联网 发布:看门狗低配置优化补丁 编辑:程序博客网 时间:2024/06/05 14:07

    从网页中通过自定义URL Protocol调用本地程序,需要将协议写到注册表中。浏览器在解析到自定义URL Protocol之后,寻找注册表,通过注册表启动相应的程序并传入参数。协议里面需要记录本地程序的路径信息。

自定义协议格式如下:

<span style="font-family:黑体;font-size:18px;"><span style="line-height: 22.383333206176758px;">Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\DTJCWF]"URL Protocol"="C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe"@="</span></span><span style="font-family:黑体;font-size:18px;"><span style="line-height: 22.383333206176758px;">CustomProtocol"[HKEY_CLASSES_ROOT\DTJCWF\DefaultIcon]@="C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe,1"[HKEY_CLASSES_ROOT\DTJCWF\shell][HKEY_CLASSES_ROOT\DTJCWF\shell\open][HKEY_CLASSES_ROOT\DTJCWF\shell\open\command]@="\"C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe\" \"%1\""</span></span>



协议格式说明:

第一行是注册表工具的版本信息(不需要改变);

第二行中的“DTJCWF”就是自定义URL Protocol的名称(2、5、7、8、9行中的该字段需要一致)修改成自己的协议名称,在web中调用的时候需要这个名称

第三行中的"C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe"是指定应用程序的路径,注意只能是exe的程序;

第四行是协议的名称:随意写,用不到;

第五行照抄;

第六行中的"C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe"也是对应的程序路径,后面的1照抄;

第七、八、九行照抄;

第十行"C:\\Users\\gengbinde\\Desktop\\dtjc_wf\\DTJCWF.exe"也是对应的程序路径,其中%1表示到参数,参数可以在你的程序simulink.exe中解析得到。


可以将上面的协议卸载一个文本文档中,然后修改该文件的后缀名为“.reg”。双击运行即可。


对于新手来说,修改可能有不完善的地方。而且程序从一个地方拷到另一个地方需要重新修改程序的路径信息。

现在写了一个小程序,将这个程序及对应的配置文件放到本地程序目录底下。双击程序,就可将协议信息写到注册表中。


配置文件(ProtocolInfo.xml)信息如下:



第一个参数是协议的名称(第四行需要修改的部分)。

第二个参数是应用程序的名称(程序目录底下可能有好几个可执行程序)。

第三个参数是自定义URL Protocol的名称(第二行中需要设置的部分)。


控制台程序如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Microsoft.Win32;using System.Xml.Serialization;using System.IO;namespace ProgramRegistration{    class Program    {        static void Main(string[] args)        {            List<ProtocolInfo> protocalInfos = ProtocolInfo.GetProtocolInfo(string.Format("{0}\\ProtocolInfo.xml", Environment.CurrentDirectory));            if(protocalInfos==null ||protocalInfos.Count==0)                Console.WriteLine("未获取协议的配置信息!请确保配置文件 ProtocolInfo.xml 在当前目录下。");            string programFullPath = string.Format("{0}\\{1}", Environment.CurrentDirectory, protocalInfos[0].ProgramName);            RegistryKey key = Registry.ClassesRoot;            RegistryKey software = key.CreateSubKey(protocalInfos[0].NodeName);            software.SetValue("URL Protocol", programFullPath);            software.SetValue("", protocalInfos[0].ProtocolName);            RegistryKey softwareDefaultIcon = software.CreateSubKey("DefaultIcon");            softwareDefaultIcon.SetValue("", string.Format("{0},{1}", programFullPath, 1));            RegistryKey softwareShell = software.CreateSubKey("shell");            softwareShell = softwareShell.CreateSubKey("open");            softwareShell = softwareShell.CreateSubKey("command");            softwareShell.SetValue("",string.Format("\"{0}\" \"%{1}\"",programFullPath,1));        }    }    /// <summary>    /// URL 协议信息    /// </summary>    [Serializable]    public class ProtocolInfo    {        /// <summary>        /// 注册表中的节点名称        /// </summary>        [XmlAttribute]        public string NodeName { get; set; }        /// <summary>        /// 程序名称        /// </summary>        [XmlAttribute]        public string ProgramName { get; set; }        /// <summary>        /// 协议名称        /// </summary>        [XmlAttribute]        public string ProtocolName { get; set; }        /// <summary>        /// 获取文件中的协议配置信息        /// </summary>        /// <param name="filepath">文件名称</param>        /// <returns>协议信息</returns>        public static List<ProtocolInfo> GetProtocolInfo(string filepath)        {            List<ProtocolInfo> protocolInfos = new List<ProtocolInfo>();            using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))            {                XmlSerializer serializer = new XmlSerializer(typeof(List<ProtocolInfo>));                protocolInfos = serializer.Deserialize(stream) as List<ProtocolInfo>;            }            return protocolInfos;        }        /// <summary>        /// 保存协议信息到配置文件中        /// </summary>        /// <param name="protocolInfos">协议信息</param>        public static void Save(List<ProtocolInfo> protocolInfos)        {            string file = "ProtocolInfo.xml";            XmlSerializer serializer = new XmlSerializer(typeof(List<ProtocolInfo>));            try            {                using (FileStream stream = new FileStream(file, FileMode.Create, FileAccess.Write))                {                    serializer.Serialize(stream, protocolInfos);                }            }            catch (Exception ex)            {                Console.WriteLine(ex.InnerException);            }        }    }}


对应 Win7 系统,如果当前用户没有管理员权限,写注册表会被拒。程序需要添加app.manifest文件,参考下列文章:点击打开


源代码下载地址:点击打开链接


0 0
原创粉丝点击