Marcus Chr Marcus Chr - 6 months ago 23
Vb.net Question

How do i make my VB.net program act on a specific protocol? (Example: 'GN://example')

I tried looking up a solution myself, but I still don't have any idea how to make my VB.NET program start up and run a sub, when a link with its protocol is used.
I'm looking for a code or help to write a code, that'll automaticly add a custom protocol.

Example: 'GN://example'

Answer

There is no part of the framework that will do it for you. You need to split the question.

First, where does Windows stores that information?

Here's the Microsoft documentation:

https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx

Second, how do I write those registry keys?

Here's the link to the Registry class in the .NET framework:

https://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.110).aspx

Here's an example on how to write those values to the registry:

' Creates the custom protocol and sets the description
Registry.ClassesRoot.CreateSubKey("GN", RegistryKeyPermissionCheck.ReadWriteSubTree).SetValue(String.Empty, "Custom Protocol Description")

Dim protocolSubKey = Registry.ClassesRoot.OpenSubKey("GN", True)

' Icon
' TODO : Update Executable Name
protocolSubKey.CreateSubKey("DefaultIcon").SetValue(String.Empty, "ExecutableName.exe,1")

' URL Protocol string value indicates that this key declares a custom pluggable protocol handler (stays empty)
protocolSubKey.SetValue("URL Protocol", "")

' Program to execute
Dim commandSubKey = protocolSubKey.CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command")

' TODO : Update path to Executable File
commandSubKey.SetValue(string.Empty, """C:\Program Files\Alert\ExecutableName.exe\"" ""%1""")

Example on how to handle the request:

Private Shared Function ProcessInput(s As String) As String
    ' TODO Verify and validate the input 
    ' string as appropriate for your application.
    Return s
End Function

Private Shared Sub Main(args As String())

    Console.WriteLine("ExecutableName.exe invoked with the following parameters." & vbCr & vbLf)
    Console.WriteLine("Raw command-line: " & vbLf & vbTab + Environment.CommandLine)

    Console.WriteLine(vbLf & vbLf & "Arguments:" & vbLf)
    For Each s As String In args
        Console.WriteLine(Convert.ToString(vbTab) & ProcessInput(s))
    Next
    Console.WriteLine(vbLf & "Press any key to continue...")
    Console.ReadKey()

End Sub

Will output:

ExecutableName.exe invoked with the following parameters.

Raw command-line:
    "C:\Program Files\Alert\ExecutableName.exe" "GN:"Hello World""


Arguments:

    alert:Hello
    World

Press any key to continue...

Making it work seamlessly with browsers

I won't repeat what's already been said in that great answer:

http://stackoverflow.com/a/24458845/755977