如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:18:16  来源:igfitidea点击:

How do I register a custom URL protocol in Windows?

windowsurlprotocolscustom-url-protocol

提问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

  1. Go to Startthen in Findtype regedit-> it should open Registry editor

  2. Click Right Mouseon HKEY_CLASSES_ROOTthen New-> Key

  1. 转到Start然后Find输入regedit- >它应该打开Registry editor

  2. 点击Right MouseHKEY_CLASSES_ROOT然后New- >Key

enter image description here

在此处输入图片说明

  1. 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 Mouseon testus-> then New-> String Valueand add URL protocolwithout value.
  1. 在主要由给您想要的网址被称为(对我来说这将是小写的名称testus://sdfsdfsdf),然后点击Right Mousetestus- >然后New- >String Value并添加URL protocol没有价值。

enter image description here

在此处输入图片说明

  1. Then add more entries like you did with protocol ( Right MouseNew-> Key) and create hierarchy like testus-> shell-> open-> commandand inside commandchange (Default)to the path where .exeyou 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"
  1. 然后像使用协议(Right MouseNew-> Key)一样添加更多条目并创建像testus-> shell-> open->这样的层次结构,command并在内部command更改要启动(Default)的路径.exe,如果要将参数传递给 exe,则将路径包装到 exe在""并添加"%1"看起来像:"c:\testing\test.exe" "%1"

enter image description here

在此处输入图片说明

  1. To test if it works go to Internet Explorer(not Chromeor Firefox) and enter testus:have_you_seen_this_manthis should fire your .exe(give you some prompts that you want to do this - say Yes) and pass into args testus://have_you_seen_this_man.
  1. 要测试它是否有效,请转到Internet Explorer(not Chromeor Firefox) 并输入testus:have_you_seen_this_man它应该触发您的.exe(给您一些您想要执行此操作的提示 - 说 Yes) 并传入 args testus://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:// 的混乱中。