C# wmi:属性返回null

C# wmi:属性返回null,c#,wmi,wmi-query,C#,Wmi,Wmi Query,我正在尝试构建一个简单的Windows窗体应用程序,它可以使用WMI(从硬盘驱动器开始)查询用户计算机的功能 到目前为止,我已经完成了这一步(hardcrivecheckresult是我自己的课程): ConnectionOptions wmiConnOpts=newconnectionoptions(); wmiConnOpts.Impersonation=ImpersonationLevel.Impersonate; wmiConnOpts.Authentication=Authentica

我正在尝试构建一个简单的Windows窗体应用程序,它可以使用WMI(从硬盘驱动器开始)查询用户计算机的功能

到目前为止,我已经完成了这一步(
hardcrivecheckresult
是我自己的课程):

ConnectionOptions wmiConnOpts=newconnectionoptions();
wmiConnOpts.Impersonation=ImpersonationLevel.Impersonate;
wmiConnOpts.Authentication=AuthenticationLevel.Default;
wmiConnOpts.EnablePrivileges=true;
ManagementObjectSearcher ManagementObjectSearcher=new ManagementObjectSearcher(@“从Win32_LogicalDisk中选择*,其中DriveType=5”);
managementObjectSearcher.Scope.Options=wmiConnOpts;
List hardDriveCheckResults=新列表();
foreach(managementObjectSearcher.Get()中的ManagementObject ManagementObject)
{
字符串hardDriveName=managementObject[“name”]。ToString();
object objFreeSpace=managementObject[“FreeSpace”];
double freeSpace=objFreeSpace==null?0d:(double)objFreeSpace;
…附加代码不相关
}
我遇到的问题是
managementObject[“FreeSpace”]
总是返回null。我怀疑这可能与WMI调用所使用的帐户的权限有关,因此我加入了
连接选项
代码,这是谷歌提供的

任务管理器告诉我这个程序是以我的帐户(管理员)的身份运行的,所以我有点不明白为什么WMI调用不会返回所有数据

managementObject[“FreeSpace”]
的调用由于权限的原因返回null,这是否正确?或者完全是别的什么


哦,对
managementObject[“name”]
的调用正确地返回了驱动器号。

好的,答案是谷歌搜索不好。查询正在DriveType=5上筛选,这是CD-ROM驱动器。我以为我在过滤硬盘


由于驱动器中没有磁盘,可用空间部分返回null。

DriveType 5用于CD。如果你确定你的意思不是“从Win32_LogicalDisk WHERE DriveType!=5中选择*”>天哪,你们太快了!而且都是对的!对不起,这是我滥用谷歌的责任。非常感谢。
ConnectionOptions wmiConnOpts = new ConnectionOptions();
wmiConnOpts.Impersonation = ImpersonationLevel.Impersonate;
wmiConnOpts.Authentication = AuthenticationLevel.Default;
wmiConnOpts.EnablePrivileges = true;
ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(@"select * from Win32_LogicalDisk WHERE DriveType = 5");
managementObjectSearcher.Scope.Options = wmiConnOpts;
List<HardDriveCheckResult> hardDriveCheckResults = new List<HardDriveCheckResult>();

foreach (ManagementObject managementObject in managementObjectSearcher.Get())
{
    string hardDriveName = managementObject["name"].ToString();
    object objFreeSpace = managementObject["FreeSpace"];
    double freeSpace = objFreeSpace == null ? 0d : (double)objFreeSpace;
    ... additional code not relevant
}