C#实现微软自带regedit编辑器打开指定注册表路径

原理

Windows注册表编辑器(regedit.exe)有一个内置功能:自动记住上次关闭时浏览的位置。这个功能是通过在注册表中存储”最后访问的路径”来实现的。

1. 记忆位置的存储

// regedit将最后访问的路径存储在这个位置:
HKEY_CURRENT_USER
└── Software
    └── Microsoft
        └── Windows
            └── CurrentVersion
                └── Applets
                    └── Regedit
                        └── LastKey (REG_SZ) = "目标路径"

2. 工作流程

预设路径 → 启动regedit → 自动导航
    ↓           ↓           ↓
设置LastKey → regedit启动 → 读取LastKey → 跳转到指定位置

3. 详细执行过程

步骤1:预设目标路径

// 在启动regedit之前,先设置它的"记忆位置"
Registry.SetValue(
    @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit", 
    "LastKey", 
    @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
);

步骤2:启动注册表编辑器

Process.Start("regedit.exe");

步骤3:regedit的自动行为

  • regedit.exe启动时,会自动检查LastKey
  • 如果LastKey存在且有效,直接导航到该路径
  • 如果LastKey不存在或无效,显示默认的”我的电脑”根节点

源码

// 1. regedit.exe 会记住上次打开的位置
// 2. 这个位置存储在:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit 的 LastKey 值中
// 3. 先设置 LastKey 为目标路径,再启动 regedit,就会自动定位到该路径

string regeditMemoryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";
Registry.SetValue(regeditMemoryKey, "LastKey", registryPath); // 预设路径
Process.Start("regedit.exe"); // 启动注册表编辑器

详细示例

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

/// <summary>
/// 注册表路径打开工具类
/// </summary>
public static class RegistryOpener
{
    /// <summary>
    /// 打开指定注册表路径
    /// </summary>
    /// <param name="registryPath">完整的注册表路径</param>
    public static void OpenRegistryPath(string registryPath)
    {
        // 设置注册表编辑器的记忆位置
        string regeditMemoryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";
        Registry.SetValue(regeditMemoryKey, "LastKey", registryPath);
        
        // 启动注册表编辑器,会自动定位到预设路径
        Process.Start("regedit.exe");
    }
}

// 使用示例
public class Example
{
    public void Demo()
    {
        // 打开程序卸载信息
        string path = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam";
        RegistryOpener.OpenRegistryPath(path);
    }
}

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇