luk*_*eck 54 windows url protocols custom-url-protocol
如何在Windows中注册自定义协议,以便在单击电子邮件或网页中的链接时打开我的应用程序并将URL中的参数传递给它?
Mat*_*ius 31
Start
然后转到Find
类型regedit
- >它应该打开Registry editor
点击Right Mouse上HKEY_CLASSES_ROOT
然后New
- >Key
testus://sdfsdfsdf
),然后点击Right Mouse上testus
- >然后New
- > String Value
并添加URL protocol
没有价值.New
- > Key
)一样添加更多条目,并创建层次结构,如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
(给你一些提示你想要这样做 - 说是)并传入args 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这能为您节省一些时间.
Jon*_*lle 29
我认为这在MSDN中有所涉及,请参阅将应用程序注册到URL协议.
MSa*_*ers 19
MSDN链接很好,但那里的安全信息不完整.处理程序注册应包含"%1",而不是%1.这是一种安全措施,因为在调用自定义协议处理程序之前,某些URL源会错误地解码%20.
PS.您将获得整个URL,而不仅仅是URL参数.但除了已经提到的%20->空间转换之外,URL可能会受到一些虐待.在URL语法设计中保守一点很有帮助.不要乱扔//或者你会陷入文件://的混乱.
duc*_*zle 18
如果有人想要用于创建关联的 .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\""
Run Code Online (Sandbox Code Playgroud)
将其粘贴到记事本中,然后文件 -> 另存为 -> duck.reg,然后运行它。运行后,当您duck://arg-here
在 chrome 中输入内容时,ConsoleApp1.exe 将以“arg-here”作为参数运行。exe 的路径需要使用双斜杠,并且必须对双引号进行转义。
在带有 Edge(chrome 版本)和 Chrome 的 Windows 11 上进行了测试和运行
有一个 npm 模块用于此目的。
链接: https: //www.npmjs.com/package/protocol-registry
因此,要在 Nodejs 中执行此操作,您只需运行以下代码:
首先安装它
npm i protocol-registry
Run Code Online (Sandbox Code Playgroud)
然后使用下面的代码注册您的条目文件。
npm i protocol-registry
Run Code Online (Sandbox Code Playgroud)
然后假设有人打开 testproto://test 然后将启动一个新终端执行:
node yourapp/index.js testproto://test
Run Code Online (Sandbox Code Playgroud)
它还支持所有其他操作系统。