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 2024-10-11 22:07  空明流光  阅读(23)  评论(0)    收藏  举报

导航