如何在Windows中注册自定义协议,以便在电子邮件或网页中单击链接时打开我的应用程序,并将URL参数传递给它?
如何在Windows中注册自定义协议,以便在电子邮件或网页中单击链接时打开我的应用程序,并将URL参数传递给它?
打开 开始
,然后在 搜索
中输入 regedit
-> 应该会打开注册表编辑器
点击 右键 在 HKEY_CLASSES_ROOT
上,然后选择 新建
-> 键
testus://sdfsdfsdf
),然后点击右键在testus
上 -> 然后选择新建
-> 字符串值
并添加URL协议
,不需要值。
然后像你在协议中所做的那样添加更多条目(右键 新建
-> 键
),并创建层次结构,如 testus
-> shell
-> open
-> command
,在 command
中将 (Default)
更改为你想要启动的 .exe
文件的路径,如果你想要向你的exe传递参数,那么将exe的路径用""
包裹起来,并添加"%1"
,看起来像这样:"c:\testing\test.exe" "%1"
。
要测试它是否有效,请打开Internet Explorer(不要用Chrome或Firefox),然后输入testus:have_you_seen_this_man
,这将启动你的.exe
(会给你一些提示,你要选择是 - 选择Yes),并将参数传递给testus://have_you_seen_this_man
。using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
if (args!= null && args.Length > 0)
Console.WriteLine(args[0]);
Console.ReadKey();
}
}
}
[已过时 - MSDN信息已被新页面替换,该页面解决了安全问题]
MSDN链接很好,但那里的安全信息并不完整。处理程序注册应包含“%1”,而不是%1。这是一项安全措施,因为某些URL来源在调用自定义协议处理程序之前不正确地解码%20。
附注:您将获得整个URL,而不仅仅是URL参数。但是,URL可能会受到某些虐待,除了已经提到的%20->空格转换之外。在URL语法设计方面保守一些会有所帮助。不要随意添加//,否则您将陷入file://的混乱中。
REG_SZ
。在内部,Windows 不解析命令行,因此空格也会被保留。但是,任何解析命令行的语言运行时(例如 C 的 argv[]
解析)都会在未引用的空格上中断。 - MSalters如果有人想要一个.reg文件来创建关联,请见下文:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\duck]
"URL Protocol"=""
[HKEY_CLASSES_ROOT\duck\shell]
[HKEY_CLASSES_ROOT\duck\shell\open]
[HKEY_CLASSES_ROOT\duck\shell\open\command]
@="\"C:\\Users\\duck\\source\\repos\\ConsoleApp1\\ConsoleApp1\\bin\\Debug\\net6.0\\ConsoleApp1.exe\" \"%1\""
将其复制到记事本中,文件 -> 另存为 -> duck.reg,然后运行它。运行后,当您在 Chrome 中键入duck://arg-here
时,ConsoleApp1.exe将以“arg-here”作为参数运行。路径需要使用双斜杠,并且双引号必须转义。在 Windows 11上测试并适用于Edge(Chrome 版本)和Chrome。reg
会覆盖它吗? - Nitin Sawant有一个用于此目的的npm模块。
链接:https://www.npmjs.com/package/protocol-registry
因此,在node.js中实现这一点,您只需要运行以下代码:
首先安装它
npm i protocol-registry
然后使用以下代码来注册你的入口文件。
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // sets protocol for your command , testproto://**
command: `node ${path.join(__dirname, './index.js')} $_URL_`, // $_URL_ will the replaces by the url used to initiate it
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true, // Use this to run your command inside a terminal
script: false
}).then(async () => {
console.log('Successfully registered');
});
node yourapp/index.js testproto://test
c:\temp\testprotocol-handler.bat
。REM just echo the passed argument
echo off
echo Hello from the custom protocol handling script.
echo script name: %0
echo script arg : %1
pause
testprotocol
。Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\testprotocol]
"URL Protocol"=""
[HKEY_CLASSES_ROOT\testprotocol\shell]
[HKEY_CLASSES_ROOT\testprotocol\shell\open]
[HKEY_CLASSES_ROOT\testprotocol\shell\open\command]
@="\"C:\\temp\\testprotocol-handler.bat\" \"%1\""
echo
语句的内容。
@echo off
setlocal
set "protocol_name=sugamprotocol"
set "application_path=C:\Users\%USERNAME%\Downloads\ping_execute.exe"
set "key_path=HKCU\SOFTWARE\Classes\%protocol_name%"
set "command_path=%key_path%\shell\open\command"
REM Create the registry key for the custom protocol handler
reg add "%key_path%" /ve /d "URL:Custom Protocol" /f > nul
reg add "%key_path%" /v "URL Protocol" /d "" /f > nul
REM Create the command key to specify the application to run
reg add "%command_path%" /ve /d "\"%application_path%\" \"%%1\"" /f > nul
echo Custom protocol handler registered successfully!
endlocal