首页 > 编程语言 >c# 注册协议处理程序

c# 注册协议处理程序

时间:2024-10-11 22:14:03浏览次数:1  
标签:SetValue String c# HKEY 处理程序 Registry 注册 RegistryValueKind string

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

 

标签:SetValue,String,c#,HKEY,处理程序,Registry,注册,RegistryValueKind,string
From: https://www.cnblogs.com/nanfei/p/18459457

相关文章

  • Centos8使用KVM
    安装好Centos8后先配置yum源[root@localhost~]#cd/etc/yum.repos.d/[[email protected]]#mkdiryum.bak[[email protected]]#lsCentOS-AppStream.repoCentOS-CR.repoCentOS-Extras.repoCentOS-Media.repoCentOS-Vault.repoCe......
  • RepVGGBlock+GSConv+Bifpn+c2fca(模块融合)改进yolo5
    参考论文:ExploringtheClose-RangeDetectionofUAV-BasedImagesonPineWiltDiseasebyanImprovedDeepLearningMethod 首先我们来介绍一下该文章使用的改进模块接着再来实现它 我们查看论文提出的repvggblock1和repvggblock2有什么区别: 可以看到只是一个bn层......
  • The 2022 ICPC Asia Hangzhou Regional Programming Contest K. Master of Both
    题目链接题意:给定n个字符串,然后给定q种字典序规则,在这q种字典序规则下求出这n个字符串的逆序对数量。思路:我们发现q很大,对于每一种排序规则都遍历一遍n个字符串去求逆序对的数量复杂度太高,所以考虑预处理。我们知道要判断两个字符串字典序的大小关系,只需要知道它们第......
  • [linux] 使用Screen后台运行命令
    概述Screen需要下载,常用来后台运行程序。比如后台运行一个nodejs项目、mc服务器等。下载在centos中,yuminstallscreen;在ubuntu中,aptinstallscreen。使用screen-h查看帮助文档查看所有会话screen-lsdaohe@neko:~/MC/Server$screen-lsTherearescreenson:......
  • ARC169D 做题记录
    link假定\(a_{1\simn}\)不对\(n\)取模,设最终状态为\(b_{1\simn}\),令\(S=\sum\limits_{i=1}^n(b_i-a_i)\),应满足以下条件:\(b_i\bmodn\)两两不同\(m|S\)\(\max\limits_{i=1}^n(b_i-a_i)\)先对\(a\)排序,那么可以发现最优情况下\(b\)也......
  • Centos7-分区扩容
    扩容前置条件:/dev/sda1,/dev/sda2,/dev/sda3三分区已存在/dev/sda3分区挂在根节点/分区扩容目标:/dev/sda3扩容(而不是新增硬盘分区)OS版本:centos7  主要扩容步骤如下:1.查看分区df-Th2.增加硬盘大小关机后,增加若干G,比如最大由20G,增加之50G3.查看磁盘扩容后状态......
  • C221110C. SolarPea与网格
    C221110C.SolarPea与网格是怎么想到dp定义的?思考下面这个情景:如果一个人在\(x\),另一个人在\(y\(x\lty)\),那么在\(x\)的人会把\(x\lti\lty\)的所有\(i\)全走一遍,走完之后\(x+1=y\)。对于这个情景,我们想到记\(f[i]\)表示一个人在\(i-1\),一个人在......
  • 《Pytorch深度学习实践》P3梯度下降法 笔记+代码+图像:梯度下降、随机梯度下降、小批量
    目录梯度下降(BatchGradientDescent)随机梯度下降(StochasticGradienDescent,SGD)小批量随机梯度下降(Mini-batchGradientDescent)梯度下降(BatchGradientDescent)介绍:使用所有的训练样本计算梯度,并且在每次迭代中更新权重。原理:假设有一个损失函数,它依赖于参数。通过最......
  • ClickHouse 数据保护指南:从备份到迁移的全流程攻略
    一、背景运行3年的clickhouse需要迁移机房,迁移单库单表的140亿条的数据。采用clickhouse-backup的方式进行备份迁移,打包备份,再加上数据拷贝,数据恢复一共花费30分钟。数据在一定量级,避免使用SQL导入导出的方式,效率太低二、clickhouse-backup工具介绍clickhouse-back......
  • Java使用idea自动生成CRUD代码
    要在IntelliJIDEA中自动生成CRUD代码,你可以使用Lombok插件和MyBatis-Plus插件。以下是具体步骤:首先,确保你已经安装了IntelliJIDEA。如果没有,请从官网下载并安装:https://www.jetbrains.com/idea/download/打开IntelliJIDEA,点击菜单栏的File>Settings(或者使用快捷键Ctrl+Alt+S),在......