I recently wanted to just this as well - and I found the answer so i'm posting it here for future reference and I couldn't find a working example in C# anywhere.
First of all your app needs requireAdministrator
permissions. To do this, right click on the project in the Solution Explorer and click Add New Item
, then select General
and finally Application Manifest
file if you don't already have one. In there, change the requestedExecutionLevel
to requireAdministrator
. Save.
I this is the first time you've done this, you'll need to restart Visual Studio as it probably isnt running under Admin privaleges.
Okay, so I wanted my app to create the registry key when it starts up, so in the constructor for my form I put in the following code, which creates the URL Protocol foo://
for a program called 'oggsplit.exe' (which I happened to have in my C: root so I just used that for testing)
RegistryKey key;
key = Registry.ClassesRoot.CreateSubKey("foo");
key.SetValue("", "URL: Foo Protocol");
key.SetValue("URL Protocol","");
key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", "C:\oggsplit.exe");
Once you've configured that, save and run the program. You'll get no feedback, and as long as you don't see any errors it should have worked correctly. Now, open your browser (no need to restart or anything) and go to the address foo://hello
. This is what it looks like for me in Google Chrome:
It will then ask you if you want to open your application from the browser, click okay. Hey Presto, your app opens from the browser, you can now put a specilised link into your web page to open your app from the browser. This Page also documents how to pass arguments through to your program as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…