C#uri协议

时间:2017-02-07 09:18:50

标签: c# winforms

我正在使用c#(ASP WinForm)开发两个应用程序

我需要从Web发送一些参数到WinForm应用程序。为此,我写了一个函数,允许我的App为这个连接创建一个URI协议:

    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");
    }

我使用这段代码来调用函数:

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

在ASP项目中,我将参数发送到我的应用程序,如下所示:

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

但是当我尝试打开这样的ASP页面时没有任何反应:

http://mydomain/BlankPage.aspx

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

从Windos 8开始,您将需要添加更多注册表项:

Registry.SetValue(
                $@"HKEY_CLASSES_ROOT\{protocolName}",
                string.Empty,
                protocolValue,
                RegistryValueKind.String);

Registry.SetValue(
                $@"HKEY_CLASSES_ROOT\{protocolName}",
                "URL Protocol",
                String.Empty,
                RegistryValueKind.String);

Registry.SetValue($@"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(
                    $@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}",
                    string.Empty,
                    protocolValue,
                    RegistryValueKind.String);

    Registry.SetValue(
       $@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\{protocolName}\shell\open\command",
        string.Empty,
        command,
        RegistryValueKind.String);

    Registry.SetValue(
       $@"HKEY_LOCAL_MACHINE\SOFTWARE\{protocolName}\Capabilities\URLAssociations",
        protocolName,
        protocolName,
        RegistryValueKind.String);

    Registry.SetValue(
        @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
        protocolName,
        $@"SOFTWARE\{protocolName}\Capabilities",
        RegistryValueKind.String);
}