注册一种自定义文件类型

 

网页端代码

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

}

posted @ 2024-08-23 20:54  网络来者  阅读(18)  评论(0)    收藏  举报