首页 > 其他分享 >注册一种自定义文件类型

注册一种自定义文件类型

时间:2024-08-23 20:53:59浏览次数:6  
标签:UpdateDefaultStringValue RegistryKey string 自定义 sppcexeKey OpenOrCreateSubKey 注册

 

网页端代码

<a href="sppcexe:PI;242700623010">PI配置</a>

 

类调用

     new 注册文件类型();

=====================================================================

操作类:

using Microsoft.Win32;
using System;
using System.Diagnostics;

public class 注册文件类型
{
     public 注册文件类型()
     {
         try
         {
             CheckAndCreateOrUpdateRegistryKeysAndValues();
         }
         catch (Exception ex)
         {

        }
     }
     public void CheckAndCreateOrUpdateRegistryKeysAndValues()
     {
         string exePath = Process.GetCurrentProcess().MainModule.FileName; // 获取当前exe程序的绝对路径
         // 定义注册表路径
         string rootKeyPath = @"HKEY_CLASSES_ROOT\sppcexe";
         string shellPath = rootKeyPath + @"\shell";
         string openPath = shellPath + @"\open";
         string commandPath = openPath + @"\command";

        // 使用Registry 类打开HKEY_CLASSES_ROOT
         RegistryKey rootKey = Registry.ClassesRoot;

        // 创建或获取注册表项 sppcexe
         RegistryKey sppcexeKey = OpenOrCreateSubKey(rootKey, "sppcexe");
         UpdateDefaultStringValue(sppcexeKey, null, $"URL:山蒲增强程序");
         UpdateDefaultStringValue(sppcexeKey, "URL Protocol", $"{exePath}");
         // 创建或获取注册表项 DefaultIcon
         RegistryKey defaultIconKey = OpenOrCreateSubKey(sppcexeKey, "DefaultIcon");

        // 更新或设置 DefaultIcon 的默认值
         UpdateDefaultStringValue(defaultIconKey, null, $"{Process.GetCurrentProcess().MainModule.FileName},1");

        // 创建或获取注册表项 shell
         RegistryKey shellKey = OpenOrCreateSubKey(sppcexeKey, "shell");

        // 创建或获取注册表项 open
         RegistryKey openKey = OpenOrCreateSubKey(shellKey, "open");

        // 创建或获取注册表项 command
         RegistryKey commandKey = OpenOrCreateSubKey(openKey, "command");

        // 更新或设置 command 的默认值
         UpdateDefaultStringValue(commandKey, null, $"\"{exePath}\" \"%1\"");


         // 关闭所有打开的注册表项
         commandKey?.Close();
         openKey?.Close();
         shellKey?.Close();
         defaultIconKey?.Close();
         sppcexeKey?.Close();
     }

    private RegistryKey OpenOrCreateSubKey(RegistryKey parentKey, string subKeyName)
     {
         RegistryKey subKey = parentKey.OpenSubKey(subKeyName, writable: true);
         if (subKey == null)
         {
             subKey = parentKey.CreateSubKey(subKeyName);
             Console.WriteLine($"注册表项 '{subKeyName}' 已创建。");
         }
         else
         {
             Console.WriteLine($"注册表项 '{subKeyName}' 已存在。");
         }
         return subKey;
     }

    private void UpdateDefaultStringValue(RegistryKey key, string xiangci, string value)
     {
         object currentValue = key.GetValue(xiangci);
         if (currentValue == null || currentValue.ToString() != value)
         {
             key.SetValue(xiangci, value, RegistryValueKind.String);
             Console.WriteLine($"注册表项 '{key.Name}' 的默认值已更新为 '{value}'。");
         }
         else
         {
             Console.WriteLine($"注册表项 '{key.Name}' 的默认值已经是 '{value}'。");
         }
     }

}

标签:UpdateDefaultStringValue,RegistryKey,string,自定义,sppcexeKey,OpenOrCreateSubKey,注册
From: https://www.cnblogs.com/hlm750908/p/18377074

相关文章

  • 应用程自定义协议与序列化反序列化
        本篇将主要介绍在应用层中自定义的协议,自定义协议的同时还需要将我们的数据继续序列化和反序列化,所以本篇的重点为序列化、反序列化的原因以及如何自定义协议,接着探讨了关于为什么tcp协议可以支持全双工协议。还根据用户自定义协议结合tcpsocket编写了一份三......
  • 【pytest】 在启动任务时将自定义参数传入代码中
    1.使用 pytest_addoption 钩子函数你可以在 conftest.py 文件中使用 pytest_addoption 钩子函数来定义自定义命令行参数。然后,你可以在你的测试文件中通过 request fixture来访问这些参数。conftest.py#contentofconftest.pyimportpytestdefpytest_ad......
  • Flutter 自定义日期范围选择组件,使用更加灵活,满足UI设计需要
    一、实现的效果图二、虽然Flutter也为我们提供了日期范围选择组件showDateRangePicker,但是毕竟系统的UI不符合我们的设计风格,所以被迫只能自己实现一个了系统样式三、日历整体实现逻辑其实也很简单,如下:首先获取每个月份具体有多少天int_getMonthDays(DateTimetim......
  • 在 Monaco Editor 中自定义右键菜单并支持多级菜单
    在MonacoEditor中自定义右键菜单能够提供更灵活的功能选项。以下是如何在MonacoEditor中实现自定义右键菜单,并支持多级菜单的步骤及关键代码示例。1.初始化MonacoEditor实例首先,需要初始化MonacoEditor实例,并设置基本的编辑器配置。constinitEditor=()=......
  • 自定义安装Nginx
    nginx下载地址:https://nginx.org/download/1.下载wgethttps://nginx.org/download/nginx-1.18.0.tar.gz2.安装依赖yum-yinstallgccgcc-c++makeautomakeautoconfpcrepcre-develzlibzlib-developensslopenssl-devellibtool3.解压tar-vxfnginx-1.18.0.tar.......
  • 自定义安装Mysql版本
    自定义安装Mysql版本mysql下载地址:https://downloads.mysql.com/archives/community/1.下载wgethttps://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar--2024-08-2010:15:39--https://downloads.mysql.com/archives/get/p/23/......
  • 关于在得帆云数据中台如何自定义函数
    UDF使用示例场景说明:使用udf编写一个函数Unit_Conversion(value)。在函数中根据value的值进行单位转化,并进行类型转化。1、导入依赖在pom.xml中将如下依赖进行导入。<dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec<......
  • WPF 模拟UWP原生窗口样式——亚克力|云母材质、自定义标题栏样式、原生DWM动画 (附我封
    先看一下最终效果,左图为使用亚克力材质并添加组合颜色的效果;右图为MicaAlt材质的效果。两者都自定义了标题栏并且最大限度地保留了DWM提供的原生窗口效果(最大化最小化、关闭出现的动画、窗口阴影、拖拽布局器等)。接下来把各部分的实现一个个拆开来讲讲。一、使用窗口材质特......
  • 探索HarmonyOS中的列表组件及其自定义特性
    在现代移动应用中,List组件是数据列表的关键元素。HarmonyOS中的List组件不仅具备传统的列表功能,还提供了丰富的自定义选项,允许开发者根据需求灵活调整列表的行为和外观展示。本文将探讨HarmonyOS中列举组件的自定义特性,包括自定义项布局、动态加载数据、多列布局、拖拽排序......
  • QT自定义结构体的传递
    方法1:使用Q_DECLARE_METATYPE(TestResult)进行声明1.1声明#ifndefTESTRESULT_H#defineTESTRESULT_H#include<QDateTime>#include<QString>#include<QMetaType>//用于测试时更新内容,作为结果导出的依据classTestResult{public:int_nid=0;int_coun......