2

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

4

3 回答 3

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;
    }
于 2012-10-23T12:55:43.760 回答
0

代码项目中的示例:

使用 C# 从注册表中读取、写入和删除

关于 C# 注册表的所有(您想知道的),第 1 部分,共 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();
于 2012-10-23T12:55:08.783 回答
0

试试这个:

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/

于 2012-10-23T12:54:25.327 回答