为什么 OpenSubKey() 在我的 Windows 10 64 位系统上返回 null?
Why is OpenSubKey() returning null on my Windows 10 64-bit system?
我正在开发 x86 应用程序以将自己创建的 "programming language" 转换为汇编程序。在某些时候,我需要获得 MASM32 的路径。我已经阅读了几个相关主题,但它们对我没有帮助,可能是因为我是 C# 的新手。
MASM32 位于此处:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MASM32
。
当我 运行 我的程序时,我总是收到 "Masm32 is not found" 消息。
我应该怎么办?提前致谢!
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(ident);
if (!myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
string user = Environment.UserDomainName + "\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.CreateSubKey | RegistryRights.WriteKey | RegistryRights.ChangePermissions | RegistryRights.SetValue,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Deny));
}
try
{
RegistryKey baseKey = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Wow6432Node\");
PathMasm32 = baseKey.GetValue("MASM32").ToString();
}
catch {
erTxt = "Masm32 is not found";
}
可能不是 OpenSubKey()
导致空引用异常,而是 baseKey.GetValue().ToString()
调用。 baseKey.GetValue()
returns null(因为在这种情况下,您正试图在 HKEY_LOCAL_MACHINE 根节点下获取一个值)并且您在 null 引用上调用 ToString()
。而不是 baseKey.GetValue()
,您应该尝试 key.GetValue()
,假设 MASM32 确实是一个低于 HKLM\SOFTWARE\Wow6432Node
的值,这是极不可能的。
key.GetValue("MASM32").ToString();
安全注意事项:如果你正在寻找MASM32的安装路径,即使我没有这方面的专业知识,他们明确指出The MASM32 SDK is registry safe and writes nothing to the registry.
因此,MASM32 可能是一个 KEY 而不是一个 VALUE,因此请执行此方法并打印它的输出,假设它存在于注册表中,您将看到在 MASM32 键下注册的 key/value 对路径 HKLM\SOFTWARE\Wow6432Node\MASM32
public static string GetMASM32LocationFromRegistry()
{
RegistryKey localMachineRegistryKey;
RegistryKey masm32RegistryKey;
RegistryView currentRegistryView = RegistryView.Registry64;
localMachineRegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, currentRegistryView);
masm32RegistryKey = localMachineRegistryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\MASM32");
if (masm32RegistryKey == null)
{
return @"ERROR: The registry key HKLM\SOFTWARE\Wow6432Node\MASM32 could not be found";
}
StringBuilder masm32RegistryKeyValuesBuilder = new StringBuilder("Key/Value pairs for registry key HKLM\SOFTWARE\Wow6432Node\MASM32:\r\n");
foreach (string masm32RegistryKeyValueName in masm32RegistryKey.GetValueNames())
{
masm32RegistryKeyValuesBuilder.AppendFormat
(
"Key: [{0}], Value: [{1}], Value Type: [{2}]\r\n",
masm32RegistryKeyValueName,
masm32RegistryKey.GetValue(masm32RegistryKeyValueName),
masm32RegistryKey.GetValueKind(masm32RegistryKeyValueName)
);
}
return masm32RegistryKeyValuesBuilder.ToString();
}
我正在开发 x86 应用程序以将自己创建的 "programming language" 转换为汇编程序。在某些时候,我需要获得 MASM32 的路径。我已经阅读了几个相关主题,但它们对我没有帮助,可能是因为我是 C# 的新手。
MASM32 位于此处:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MASM32
。
当我 运行 我的程序时,我总是收到 "Masm32 is not found" 消息。
我应该怎么办?提前致谢!
WindowsIdentity ident = WindowsIdentity.GetCurrent();
WindowsPrincipal myPrincipal = new WindowsPrincipal(ident);
if (!myPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
{
string user = Environment.UserDomainName + "\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.CreateSubKey | RegistryRights.WriteKey | RegistryRights.ChangePermissions | RegistryRights.SetValue,
InheritanceFlags.None,
PropagationFlags.None,
AccessControlType.Deny));
}
try
{
RegistryKey baseKey = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,
RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey(@"SOFTWARE\Wow6432Node\");
PathMasm32 = baseKey.GetValue("MASM32").ToString();
}
catch {
erTxt = "Masm32 is not found";
}
可能不是 OpenSubKey()
导致空引用异常,而是 baseKey.GetValue().ToString()
调用。 baseKey.GetValue()
returns null(因为在这种情况下,您正试图在 HKEY_LOCAL_MACHINE 根节点下获取一个值)并且您在 null 引用上调用 ToString()
。而不是 baseKey.GetValue()
,您应该尝试 key.GetValue()
,假设 MASM32 确实是一个低于 HKLM\SOFTWARE\Wow6432Node
的值,这是极不可能的。
key.GetValue("MASM32").ToString();
安全注意事项:如果你正在寻找MASM32的安装路径,即使我没有这方面的专业知识,他们明确指出The MASM32 SDK is registry safe and writes nothing to the registry.
因此,MASM32 可能是一个 KEY 而不是一个 VALUE,因此请执行此方法并打印它的输出,假设它存在于注册表中,您将看到在 MASM32 键下注册的 key/value 对路径 HKLM\SOFTWARE\Wow6432Node\MASM32
public static string GetMASM32LocationFromRegistry()
{
RegistryKey localMachineRegistryKey;
RegistryKey masm32RegistryKey;
RegistryView currentRegistryView = RegistryView.Registry64;
localMachineRegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, currentRegistryView);
masm32RegistryKey = localMachineRegistryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\MASM32");
if (masm32RegistryKey == null)
{
return @"ERROR: The registry key HKLM\SOFTWARE\Wow6432Node\MASM32 could not be found";
}
StringBuilder masm32RegistryKeyValuesBuilder = new StringBuilder("Key/Value pairs for registry key HKLM\SOFTWARE\Wow6432Node\MASM32:\r\n");
foreach (string masm32RegistryKeyValueName in masm32RegistryKey.GetValueNames())
{
masm32RegistryKeyValuesBuilder.AppendFormat
(
"Key: [{0}], Value: [{1}], Value Type: [{2}]\r\n",
masm32RegistryKeyValueName,
masm32RegistryKey.GetValue(masm32RegistryKeyValueName),
masm32RegistryKey.GetValueKind(masm32RegistryKeyValueName)
);
}
return masm32RegistryKeyValuesBuilder.ToString();
}