DevELopE DevELopE - 1 year ago 116
C# Question

C# uri protocol

I'm developing two apps with c# (ASP WinForm)

I need to send some parameters from web to the WinForm app. for this, I wrote a function that allows my App to create an URI Protocol for this connection:

public static void RegisterURLProtocol(string protocolName, string applicationPath, string description)
{
RegistryKey myKey = Registry.ClassesRoot.CreateSubKey(protocolName);
myKey.SetValue(null, description);
myKey.SetValue("URL Protocol", string.Empty);
Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell");
Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open");
myKey = Registry.ClassesRoot.CreateSubKey(protocolName + "\\Shell\\open\\command");
myKey.SetValue(null, "\"" + applicationPath+ "\" %1");
}


I use this peace of code to call the function:

RegisterURLProtocol("mAPP", Application.ExecutablePath, "mAPP Uri Protocol");


in ASP project i send parameters to my app like this:

protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("mAPP://MYPARAMETERS");
}


But nothing happens when i try to open my ASP page like this:

http://mydomain/BlankPage.aspx

how can I fix this?

Answer Source

From Windos 8 onwards you will have to add some more registry keys:

Registry.SetValue(
                [email protected]"HKEY_CLASSES_ROOT\{protocolName}",
                string.Empty,
                protocolValue,
                RegistryValueKind.String);

Registry.SetValue(
                [email protected]"HKEY_CLASSES_ROOT\{protocolName}",
                "URL Protocol",
                String.Empty,
                RegistryValueKind.String);

Registry.SetValue([email protected]"HKEY_CLASSES_ROOT\{protocolName}\shell\open\command", string.Empty, command, RegistryValueKind.String);


// detect win 8 and register as choosable protocol handler
Version win8Version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
    Environment.OSVersion.Version >= win8Version)
{
     Registry.SetValue(
                    [email protected]"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}",
                    string.Empty,
                    protocolValue,
                    RegistryValueKind.String);

    Registry.SetValue(
       [email protected]"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}\shell\open\command",
        string.Empty,
        command,
        RegistryValueKind.String);

    Registry.SetValue(
       [email protected]"HKEY_LOCAL_MACHINE\SOFTWARE\{protocolName}\Capabilities\URLAssociations",
        protocolName,
        protocolName,
        RegistryValueKind.String);

    Registry.SetValue(
        @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
        protocolName,
        [email protected]"SOFTWARE\{protocolName}\Capabilities",
        RegistryValueKind.String);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download