This tutorial applies to Windows systems
Open the local program from the browser, the main use of the local URL protocol, in fact, mainly to change the registry
First build a project, I built a console project
Write 2 static methods in Program.cs to add the registry, remove the registry (the main way to open a program in a browser is by both methods)
/// <summary> ///registering startup entries to the registry/// </summary> Public Static voidReg () {//Register the protocol header, that is, the path in the address bar such as QQ: Tencent://xxxxx/xxx I'm registering June. In the Address bar, enter: June://You can open this program . varSurekamkey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey ("June"); //The following parameters are fixed and do not need to be changed to copy the past directly varShellkey = Surekamkey.createsubkey ("Shell"); varOpenkey = Shellkey.createsubkey ("Open"); varCommandkey = Openkey.createsubkey ("Command"); Surekamkey.setvalue ("URL Protocol",""); //here the executable file takes the full path of the current program, can be modified as needed varExePath =process.getcurrentprocess (). Mainmodule.filename; Commandkey.setvalue ("","\""+ ExePath +"\""+"\ "%1\""); }
/// <summary> /// Cancel Registration /// </summary> Public Static void Unreg () { // Delete node directly Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree ("June"); }
And then add some logic to the main method to invoke the two methods
Static void Main (string[] args) { Console.WriteLine (" Input: 1 Registration Protocol 2 Uninstall protocol " ); if " 1 " ) Reg (); Else Unreg (); }
Then compile, debug, run the program input 1, hit Enter, the program seconds back, which indicates that has been successful, first enter the registry to see if there is no added success
Open registry (run: regedit): Hkey_classes_root\jun
You can see that the registry key has been added successfully and then test it for normal use:
If you signed up for June, click here to open your program.
Otherwise, enter in the browser address bar: protocol name://such as: jun://hit Enter (many browsers can not directly input, to use a tag jump, ie can be directly input, the proposed use of IE test)
Open the program from the browser has been implemented, then the parameters, how to pass the parameter, in fact, it is quite simple, then we take the Main method parameters (through args):
Change the Main method to:
Static voidMain (string[] args) { if(args. Length >0) {Console.WriteLine (args[0]); } Console.WriteLine ("Input: 1 Registration Agreement 2 Uninstall Protocol"); if(Console.ReadLine () = ="1") Reg (); ElseUnreg (); }
Compile and then browser input: protocol name://hello,world
If you signed up for June, click here to open your program.
Results such as:
The above is the obtained parameter, which can be obtained by splitting the string.
Uninstall we will not test, the method has been provided, directly delete the registry on the line
SOURCE download
C # Registration URL protocol, using a browser to open a local program, similar to the point on a Web page QQ chat open local QQ client program