如何解决当注册表项包含空格时,注册表值返回null
当尝试从远程计算机中检索注册表项的值(对于在C#中使用WMI使用空格的键)(使用WMI的原因:需要身份验证和模拟)时,GetStringValue返回null,但是当键不存在时有空间,效果很好。对字符串使用@表示法或标准“表示法都无济于事。我尝试将键括在双引号中。也无济于事。
这是我编写的代码:
public static string GetRemoteRegistryValue(string MachineName,string username,string password)
{
string regValue = string.Empty;
ConnectionOptions opt = new ConnectionOptions();
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
opt.Username = username;
opt.Password = password;
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
try
{
ManagementPath p = new ManagementPath("\\\\" + MachineName + "\\root\\cimv2");
ManagementScope msc = new ManagementScope(p,opt);
msc.Connect();
string softwareRegLoc = "\"SOFTWARE\\VMware,Inc.\\VMware Drivers\"";
//string softwareRegLoc = @"""SOFTWARE\SAP BusinessObjects\Suite XI 4.0\Config Manager""";
ManagementClass registry = new ManagementClass(msc,new ManagementPath("StdRegProv"),null);
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
inParams["sSubKeyName"] = softwareRegLoc;
inParams["sValueName"] = "VmciHostDevInst";
// Read Registry Key Names
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue",inParams,null);
if (outParams.Properties["sValue"].Value != null)
{
regValue = outParams.Properties["sValue"].Value.ToString();
}
}
catch (ManagementException Ex)
{
}
catch (System.UnauthorizedAccessException Ex)
{
}
catch (Exception Ex)
{
}
return regValue;
}
该问题的解决方案是什么?
解决方法
好的,这里有两点:
-
您不应使用引号。因此,将
"\"SOFTWARE\\VMware,Inc.\\VMware Drivers\""
替换为"SOFTWARE\\VMware,Inc.\\VMware Drivers"
。 -
您尝试访问的路径属于64位提供程序。为了能够访问它(默认),您的应用程序需要将其平台目标设置为
x64
。否则,您的应用程序将尝试访问HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware,Inc.\VMware Drivers
路径,该路径可能不存在。
删除引号并定位x64
对我来说效果很好,我得到了问题中提到的确切路径的价值。
如果您的平台目标设置为x86
(或选中了Any CPU
的{{1}}且您不想将其更改为Prefer 32-bit
,那么您必须强制WMI访问64位注册表配置单元。检查the documentation了解更多信息。
这是一个完整的例子:
x64
当应用定位到public static string GetRemoteRegistryValue(string MachineName,string username,string password)
{
string regValue = string.Empty;
ConnectionOptions opt = new ConnectionOptions();
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
opt.Username = username;
opt.Password = password;
opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
try
{
ManagementPath p = new ManagementPath("\\\\" + MachineName + "\\root\\cimv2");
ManagementScope msc = new ManagementScope(p,opt);
msc.Connect();
string softwareRegLoc = "SOFTWARE\\VMware,Inc.\\VMware Drivers";
ManagementClass registry = new ManagementClass(msc,new ManagementPath("StdRegProv"),null);
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
inParams["sSubKeyName"] = softwareRegLoc;
inParams["sValueName"] = "VmciHostDevInst";
ManagementNamedValueCollection objCtx = new ManagementNamedValueCollection();
objCtx.Add("__ProviderArchitecture",64);
objCtx.Add("__RequiredArchitecture",true);
InvokeMethodOptions options = new InvokeMethodOptions(objCtx,TimeSpan.MaxValue);
// Read Registry Key Names
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue",inParams,options);
if (outParams.Properties["sValue"].Value != null)
{
regValue = outParams.Properties["sValue"].Value.ToString();
}
}
catch (ManagementException Ex)
{
throw;
}
catch (System.UnauthorizedAccessException Ex)
{
throw;
}
catch (Exception Ex)
{
throw;
}
return regValue;
}
时,上面的代码返回了VmciHostDevInst
的值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。