如何在 Windows 中注册自定义 URL 协议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/80650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I register a custom URL protocol in Windows?
提问by lukeck
How do I register a custom protocol with Windows so that when clicking a link in an email or on a web page my application is opened and the parameters from the URL are passed to it?
如何在 Windows 中注册自定义协议,以便在单击电子邮件或网页中的链接时打开我的应用程序并将 URL 中的参数传递给它?
回答by Matas Vaitkevicius
Go to
Start
then inFind
typeregedit
-> it should openRegistry editor
Click Right Mouseon
HKEY_CLASSES_ROOT
thenNew
->Key
转到
Start
然后Find
输入regedit
- >它应该打开Registry editor
点击Right Mouse上
HKEY_CLASSES_ROOT
然后New
- >Key
- In the Key give the lowercase name by which you want urls to be called (in my case it will be
testus://sdfsdfsdf
) then Click Right Mouseontestus
-> thenNew
->String Value
and addURL protocol
without value.
- 在主要由给您想要的网址被称为(对我来说这将是小写的名称
testus://sdfsdfsdf
),然后点击Right Mouse上testus
- >然后New
- >String Value
并添加URL protocol
没有价值。
- Then add more entries like you did with protocol ( Right Mouse
New
->Key
) and create hierarchy liketestus
->shell
->open
->command
and insidecommand
change(Default)
to the path where.exe
you want to launch is, if you want to pass parameters to your exe then wrap path to exe in""
and add"%1"
to look like:"c:\testing\test.exe" "%1"
- 然后像使用协议(Right Mouse
New
->Key
)一样添加更多条目并创建像testus
->shell
->open
->这样的层次结构,command
并在内部command
更改要启动(Default)
的路径.exe
,如果要将参数传递给 exe,则将路径包装到 exe在""
并添加"%1"
看起来像:"c:\testing\test.exe" "%1"
- To test if it works go to
Internet Explorer
(notChrome
orFirefox
) and entertestus:have_you_seen_this_man
this should fire your.exe
(give you some prompts that you want to do this - say Yes) and pass into argstestus://have_you_seen_this_man
.
- 要测试它是否有效,请转到
Internet Explorer
(notChrome
orFirefox
) 并输入testus:have_you_seen_this_man
它应该触发您的.exe
(给您一些您想要执行此操作的提示 - 说 Yes) 并传入 argstestus://have_you_seen_this_man
。
Here's sample console app to test:
这是要测试的示例控制台应用程序:
using System;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
if (args!= null && args.Length > 0)
Console.WriteLine(args[0]);
Console.ReadKey();
}
}
}
Hope this saves you some time.
希望这可以为您节省一些时间。
回答by Jonas Gulle
I think this is covered in MSDN, please see Registering an Application to a URL Protocol.
我认为这在 MSDN 中有介绍,请参阅将应用程序注册到 URL 协议。
回答by MSalters
The MSDN link is nice, but the security information there isn't complete. The handler registration should contain "%1", not %1. This is a security measure, because some URL sources incorrectly decode %20 before invoking your custom protocol handler.
MSDN 链接很好,但那里的安全信息不完整。处理程序注册应包含“%1”,而不是 %1。这是一种安全措施,因为某些 URL 源在调用您的自定义协议处理程序之前错误地解码了 %20。
PS. You'll get the entire URL, not just the URL parameters. But the URL might be subject to some mistreatment, besides the already mentioned %20->space conversion. It helps to be conservative in your URL syntax design. Don't throw in random // or you'll get into the mess that file:// is.
附注。您将获得整个 URL,而不仅仅是 URL 参数。但是除了已经提到的 %20->space 转换之外,URL 可能会受到一些虐待。在您的 URL 语法设计中保持保守是有帮助的。不要随意输入 // 否则你会陷入 file:// 的混乱中。