从注册表中读取价值

时间:2012-10-23 12:52:06

标签: c# .net registry

谷歌今天没有帮助我,所以我需要你的帮助。有可能从注册表中读取值吗?例如这个位置 - > HKEY_LOCAL_MACHINE>软件> MyKey列是Test键

3 个答案:

答案 0 :(得分:11)

64位+ 32位

   using Microsoft.Win32;
    public static string GetRegistry()
    {
        string registryValue = string.Empty;
        RegistryKey localKey = null;
        if (Environment.Is64BitOperatingSystem)
        {
            localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
        }
        else
        {
            localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
        }

        try
        {
            localKey = localKey.OpenSubKey(@"Software\\MyKey");
            registryValue = localKey.GetValue("TestKey").ToString();
        }
        catch (NullReferenceException nre)
        {

        }
        return registryValue;
    }

答案 1 :(得分:0)

rtry this:

static object GetRegistryValue(string fullPath, object defaultValue)
{
    string keyName = Path.GetDirectoryName(fullPath);
    string valueName = Path.GetFileName(fullPath);
    return Registry.GetValue(keyName, valueName, defaultValue);
}

或者您可以使用Registry.LocalMachine 仔细阅读http://www.csharphelp.com/2007/01/registry-ins-and-outs-using-c/

答案 2 :(得分:0)

代码项目中的示例:

Read, write and delete from registry with C#

All (you wanted to know) about the Registry with C#, Part 1 of 2

using Microsoft.Win32;
...

RegistryKey masterKey = Registry.LocalMachine.CreateSubKey(
            "SOFTWARE\\Test\\Preferences");
if (masterKey == null)
{
    Console.WriteLine ("Null Masterkey!");
}
else
{
    Console.WriteLine ("MyKey = {0}", masterKey.GetValue ("MyKey"));
}
masterKey.Close();