问题
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?
回答1:
Go to
Start
then inFind
typeregedit
-> it should openRegistry editor
Click Right Mouse on
HKEY_CLASSES_ROOT
thenNew
->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 Mouse ontestus
-> thenNew
->String Value
and addURL protocol
without value.
- 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"
- 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
.
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.
回答2:
I think this is covered in MSDN, please see Registering an Application to a URL Protocol.
回答3:
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.
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.
来源:https://stackoverflow.com/questions/80650/how-do-i-register-a-custom-url-protocol-in-windows