繁体   English   中英

当键有空格时注册表值返回 null uwing WMI

[英]Registry value returns null when key has spaces uwing WMI

在 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;
}

这个问题的解决方案是什么?

好的,这里有两点:

  1. 你不应该使用引号。 因此,将"\"SOFTWARE\\VMware, Inc.\\VMware Drivers\""替换为"SOFTWARE\\VMware, Inc.\\VMware Drivers"

  2. 您尝试访问的路径属于 64 位提供程序。 为了能够访问它(默认情况下),您的应用程序需要将其 Platform Target 设置为x64 否则,您的应用程序将尝试访问HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware Drivers路径,该路径可能不存在。

删除引号并以x64为目标对我来说效果很好,我得到了问题中提到的确切路径的值。

如果您的 Platform Target 设置为x86 (或勾选Prefer 32-bit复选框的Any CPU )并且您不想将其更改为x64 ,那么您必须强制 WMI 访问 64 位注册表 Hive。 检查文档以获取更多信息。

这是一个完整的例子:

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的值,而应用程序的目标是x86

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM