c# 注册协议处理程序

复制代码
static void registerAsHttpHandler()
{
    // Register as the default handler for the http: protocol.
    const string protocolValue = "HTTP:Hypertext Transfer Protocol";
    Registry.SetValue(
        @"HKEY_CLASSES_ROOT\http",
        string.Empty,
        protocolValue,
        RegistryValueKind.String);
    Registry.SetValue(
        @"HKEY_CLASSES_ROOT\http",
        "URL Protocol",
        String.Empty,
        RegistryValueKind.String);

    string binaryName = Path.GetFileName(Application.ExecutablePath);
    string command = string.Format("\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, binaryName);
    Registry.SetValue(@"HKEY_CLASSES_ROOT\http\shell\open\command", string.Empty, command, RegistryValueKind.String);

    // For Windows 8+, register as a choosable protocol handler.

    // Version detection from https://stackoverflow.com/a/17796139/259953
    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\HttpProtocolHandler",
            string.Empty,
            protocolValue,
            RegistryValueKind.String);
        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpProtocolHandler\shell\open\command",
            string.Empty,
            command,
            RegistryValueKind.String);

        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\HttpProtocolHandler\Capabilities\URLAssociations",
            "http",
            "HttpProtocolHandler",
            RegistryValueKind.String);
        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
            "HttpProtocolHandler",
            @"SOFTWARE\HttpProtocolHandler\Capabilities",
            RegistryValueKind.String);
    }
}

static void unregisterAsHttpHandler()
{
    // 删除 http 协议的注册键
    Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\http", false);

    // 删除 HKEY_LOCAL_MACHINE 下的 http 处理程序注册
    Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\Classes\HttpProtocolHandler", false);

    // 如果需要,可以删除其他相关的键
    RegistryKey registeredAppsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegisteredApplications", true);
    if (registeredAppsKey != null)
    {
        registeredAppsKey.DeleteValue("HttpProtocolHandler", false);
        registeredAppsKey.Close();
    }
}

static void registerAsHttpsHandler()
{
    // Register as the default handler for the https: protocol.
    const string protocolValue = "HTTPS:Hypertext Transfer Protocol Secure";
    Registry.SetValue(
        @"HKEY_CLASSES_ROOT\https",
        string.Empty,
        protocolValue,
        RegistryValueKind.String);
    Registry.SetValue(
        @"HKEY_CLASSES_ROOT\https",
        "URL Protocol",
        String.Empty,
        RegistryValueKind.String);

    string binaryName = Path.GetFileName(Application.ExecutablePath);
    string command = string.Format("\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, binaryName);
    Registry.SetValue(@"HKEY_CLASSES_ROOT\https\shell\open\command", string.Empty, command, RegistryValueKind.String);

    // For Windows 8+, register as a choosable protocol handler.

    // Version detection from https://stackoverflow.com/a/17796139/259953
    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\HttpsProtocolHandler",
            string.Empty,
            protocolValue,
            RegistryValueKind.String);
        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\HttpsProtocolHandler\shell\open\command",
            string.Empty,
            command,
            RegistryValueKind.String);

        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\HttpsProtocolHandler\Capabilities\URLAssociations",
            "https",
            "HttpsProtocolHandler",
            RegistryValueKind.String);
        Registry.SetValue(
            @"HKEY_LOCAL_MACHINE\SOFTWARE\RegisteredApplications",
            "HttpsProtocolHandler",
            @"SOFTWARE\HttpsProtocolHandler\Capabilities",
            RegistryValueKind.String);
    }
}

static void unregisterAsHttpsHandler()
{
    // 删除 http 协议的注册键
    Registry.CurrentUser.DeleteSubKeyTree(@"Software\Classes\https", false);

    // 删除 HKEY_LOCAL_MACHINE 下的 http 处理程序注册
    Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\Classes\HttpsProtocolHandler", false);

    // 如果需要,可以删除其他相关的键
    RegistryKey registeredAppsKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\RegisteredApplications", true);
    if (registeredAppsKey != null)
    {
        registeredAppsKey.DeleteValue("HttpsProtocolHandler", false);
        registeredAppsKey.Close();
    }
}
复制代码

 

posted on   空明流光  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2016-10-11 asp.net 自定义控件 嵌入资源文件 备忘
2014-10-11 vb6获取字符串长度

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示