C#注册表搜索,从结果反向遍历

时间:2012-02-19 10:10:11

标签: c# windows-phone-7 dword registrykey

在找到并使用注册表编辑后,我正在弄乱我的Windows Phone,以允许通过资源管理器访问它。我的目标是编写一个快速控制台应用程序,以便在插入的任何计算机上执行此注册表编辑。

这是WINDOWS 7 REGISTRY。不是电话 Here are the steps I am trying to follow

到目前为止,我编写的代码将查找包含'ZuneDriver'的设备注册表项的所有实例

RegistryKey start = Registry.LocalMachine;
    using (RegistryKey root = start.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USB"))
    {
        string myKey = "ZuneDriver";
        SearchSubKeys(root, myKey);

这里的问题是,注册表项'ZuneDriver'是'Device Parameters'键的一个子项,我需要更改一个值。

目前,搜索结果存储在:

    static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();

通过我的搜索功能:

public static void SearchSubKeys(RegistryKey root, String searchKey)
{
    foreach (string keyname in root.GetSubKeyNames())
    {
        try
        {
            using (RegistryKey key = root.OpenSubKey(keyname))
            {
                if (keyname == searchKey)
                {
                    log.Add(key.Name);
                }


                SearchSubKeys(key, searchKey);
            }
        }
        catch (System.Security.SecurityException)
        {
        }
    }
}

存储的结果如下所示:

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Enum \ USB \ VID_045E& PID_04EC& MI_00 \ 7& b85dba6& 0& 0000 \ Device Parameters \ ZuneDriver

我想让我的程序具有以下注册表访问权限:

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Enum \ USB \ VID_045E& PID_04EC& MI_00 \ 7& b85dba6& 0& 0000 \ Device Parameters

编辑名为“ShowInShell”

的DWORD的值

但它需要通过初始搜索确定\ VID_045E& PID_04EC& MI_00 \ 7& b85dba6& 0& 0000 \ ......

任何指针或建议都非常感激:)

干杯,

1 个答案:

答案 0 :(得分:0)

我会这样做:

public static void SearchSubKeys(RegistryKey root, String searchKey)
{
    bool containsKey = false;
    foreach (string keyname in root.GetSubKeyNames())
    {
        try
        {
            using (RegistryKey key = root.OpenSubKey(keyname))
            {
                if (keyname == searchKey)
                {
                    containsKey = true;
                }

                SearchSubKeys(key, searchKey);
            }
        }
        catch (System.Security.SecurityException)
        {
        }
    }
    if(containsKey){
        using (RegistryKey key = root.CreateSubKey("Device Parameters"))
        {
            key.SetValue("ShowInShell", /* your value */, RegistryValueKind.DWord);
        }
    }
}