Puede ocurrir si se encuentra en una máquina de 64 bits. Crear una clase de ayuda en primer lugar (requiere .NET 4.0 o posterior):
public class RegistryHelpers
{
public static RegistryKey GetRegistryKey()
{
return GetRegistryKey(null);
}
public static RegistryKey GetRegistryKey(string keyPath)
{
RegistryKey localMachineRegistry
= RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32);
return string.IsNullOrEmpty(keyPath)
? localMachineRegistry
: localMachineRegistry.OpenSubKey(keyPath);
}
public static object GetRegistryValue(string keyPath, string keyName)
{
RegistryKey registry = GetRegistryKey(keyPath);
return registry.GetValue(keyName);
}
}
Uso:
string keyPath = @"SOFTWARE\MyApp\Settings";
string keyName = "MyAppConnectionStringKey";
object connectionString = RegistryHelpers.GetRegistryValue(keyPath, keyName);
Console.WriteLine(connectionString);
Console.ReadLine();
¿Usted está escapando adecuadamente KeyPath? ¿Es "SOFTWARE \\ TestKey", y no "SOFTWARE \ TestKey"? – Michael
sí, lo publiqué mal. En realidad está configurado para usar \\ – PositiveGuy
Así que esto ha funcionado bien en la PC de mi jefe. No hay razón por la que no debería estar aquí. Le di acceso a la cuenta asp.net porque estoy ejecutando el servidor web VS y todavía devuelve nulo. – PositiveGuy