注册表读取键值问题
string ACCPath = TryGetSoftwarePath(@"SOFTWARE\Microsoft\Office\14.0\Access\InstallRoot", @"Path");
private static string TryGetSoftwarePath(string regPath, string keyName)
{
RegistryKey regKey = null;
RegistryKey regSubKey = null;
try
{
//Read the key
regKey = Registry.LocalMachine;
regSubKey = regKey.OpenSubKey(regPath, RegistryKeyPermissionCheck.ReadSubTree, RegistryRights.ReadKey);
//Read the path
if (regSubKey != null)
{
var objResult = regSubKey.GetValue(keyName);
var regValueKind = regSubKey.GetValueKind(keyName);
if (regValueKind == RegistryValueKind.String)
{
return objResult.ToString();
}
}
}
catch (System.Security.SecurityException ex)
{
throw new System.Security.SecurityException("You have no right to read the registry!", ex);
}
catch (Exception ex)
{
throw new Exception("Reading registry error!", ex);
}
finally
{
if (regKey != null)
{
regKey.Close();
}
if (regSubKey != null)
{
regSubKey.Close();
}
}
return "";
}
为什么上面的键值不存在,还能正常读取呢,哪位能给我解惑下。