如何在Windows中注册自定义协议,以便在单击电子邮件或网页中的链接时打开我的应用程序并将URL中的参数传递给它?
答案 0 :(得分:26)
我认为这已在MSDN中介绍,请参阅Registering an Application to a URL Protocol。
答案 1 :(得分:21)
转到Start
然后在Find
中输入regedit
- >它应该打开Registry editor
点击HKEY_CLASSES_ROOT
上的右鼠标然后New
- > Key
testus://sdfsdfsdf
),然后点击testus
上的右鼠标 - &gt ;然后New
- > String Value
并添加URL protocol
没有价值。New
- > Key
)一样,并创建类似testus
- >的层次结构shell
- > open
- > command
和command
内(Default)
将.exe
更改为要启动""
的路径,如果要将参数传递给exe,则将路径包装到{{{} 1}}并添加"%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();
}
}
}
希望这可以节省你一些时间。
答案 2 :(得分:19)
MSDN链接很好,但那里的安全信息不完整。处理程序注册应包含“%1”,而不是%1。这是一种安全措施,因为在调用自定义协议处理程序之前,某些URL源会错误地解码%20。
PS。您将获得整个URL,而不仅仅是URL参数。但除了已经提到的%20->空间转换之外,URL可能会受到一些虐待。在URL语法设计中保守一点很有帮助。不要乱扔//或者你会陷入文件的混乱://是。
答案 3 :(得分:2)
有一个用于此目的的 npm 模块。
链接:https://www.npmjs.com/package/protocol-registry
因此,要在 nodejs 中执行此操作,您只需要运行以下代码:
首先安装它
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');
});
然后假设有人打开 testproto://test 然后将启动一个新终端执行:
node yourapp/index.js testproto://test
它还支持所有其他操作系统。