从网页中通过自定义URL Protocol调用本地程序,需要将协议写到注册表中。
浏览器在解析到自定义URL Protocol之后,寻找注册表,通过注册表启动相应的程序并传入参数。
协议里面需要记录本地程序的路径信息。
HTML调用方式如下:
<a href="Micro.Live://">WebExe,启动Exe应用程序</a>
注册表格式如下:
常用的注册三种编辑注册表的方式如下
方式一、注册表文件格式如下:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\MS] "URL Protocol"="C:\\MS.exe" @="MSProtocol" [HKEY_CLASSES_ROOT\WST\DefaultIcon] @="C:\\MS.exe,1" [HKEY_CLASSES_ROOT\WST\shell] [HKEY_CLASSES_ROOT\WST\shell\open] [HKEY_CLASSES_ROOT\WST\shell\open\command] @="\"C:\\MS.exe\"\"%1\""
复制到txt文件,然后另存为ms.reg文件,打开运行文件;
扫描二维码关注公众号,回复:
5619097 查看本文章
方式二、控制台程序(C#)如下:
try { List<ProtocolInfo> protocalInfos = ProtocolInfo.GetProtocolInfo(string.Format("{0}\\ProtocolInfo.xml", Environment.CurrentDirectory)); if (protocalInfos == null || protocalInfos.Count == 0) Console.WriteLine("未获取协议的配置信息!请确保配置文件 ProtocolInfo.xml 在当前目录下。"); string nodeName = protocalInfos[0].NodeName; string programFullPath = string.Format("{0}\\{1}", Environment.CurrentDirectory, nodeName); RegistryKey key = Registry.ClassesRoot; string a = (string)key.GetValue(nodeName, true); if (!key.Name.Contains(nodeName)) { 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)); } } catch(Exception ex) { Console.Write(ex.Message); }
方式三、部署添加注册表(C#)如下:
注册表页面,各个节点的键值为:
键(Key) | 名称(Name) | 值(Value) |
Micro.Live | WSTProtocol | |
Micro.Live | URL Protocol | C:\WSTStart.exe |
Micro.Live/DefaultIcon | C:\WSTStart.exe,1 | |
Micro.Live/shell | ||
Micro.Live/shell/open | ||
Micro.Live/shell/open/command | "C:\WSTStart.exe""%1" |