C#中的WMI问题 [英] Problem with WMI in C#

查看:108
本文介绍了C#中的WMI问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C#中使用WMI来查询连接的硬盘信息。基本上,我试图将Powershell脚本转换为C#。但是,我撞墙了。在Powershell中,我有一行:



I am trying to use WMI in C# to query the connected hard disk information. Basically, I am trying to convert a Powershell script to C#. However, I've hit a wall. In Powershell, I have a line:

$testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter='$DriveLetter'"





现在,在C#中,以下工作:





Now, in C#, the following works:

var a = new ManagementObject("Win32_LogicalDisk.DeviceID='D:'");            
Console.WriteLine(a.GetPropertyValue("VolumeSerialNumber").ToString());





但这不是





but this doesn't

var a = new ManagementObject("Win32_Volume.DriveLetter='D:'");            
Console.WriteLine(a.GetPropertyValue("DeviceID").ToString());





我得到



I get

Unhandled Exception: System.Management.ManagementException: Invalid object path

错误。



我做错了什么?



我尝试了什么:



我试过在管理员中运行Visual Studio模式,但这没有帮助。是通过构建查询字符串和使用ManagementObjectSearcher来查询Win32_Volume的唯一方法吗?我不能使用LINQ样式的扩展方法,可以使用Win32_LogicalDisk吗?

error.

What am I doing wrong?

What I have tried:

I have tried running Visual Studio in administrator mode, but that doesn't help. Is the only way to query Win32_Volume by building query strings and using ManagementObjectSearcher? Can't I do it using LINQ-style extension methods as can be done with Win32_LogicalDisk?

推荐答案

testdrv = Get-WmiObject -Class win32_volume -FilterDriveLetter ='
testdrv = Get-WmiObject -Class win32_volume -Filter "DriveLetter='


DriveLetter'
DriveLetter'"





现在,在C#中,以下工作:





Now, in C#, the following works:

var a = new ManagementObject("Win32_LogicalDisk.DeviceID='D:'");            
Console.WriteLine(a.GetPropertyValue("VolumeSerialNumber").ToString());





但这不是





but this doesn't

var a = new ManagementObject("Win32_Volume.DriveLetter='D:'");            
Console.WriteLine(a.GetPropertyValue("DeviceID").ToString());





我得到



I get

Unhandled Exception: System.Management.ManagementException: Invalid object path

错误。



我做错了什么?



我尝试了什么:



我试过在管理员中运行Visual Studio模式,但这没有帮助。是通过构建查询字符串和使用ManagementObjectSearcher来查询Win32_Volume的唯一方法吗?我不能像使用Win32_LogicalDisk那样使用LINQ样式的扩展方法吗?

error.

What am I doing wrong?

What I have tried:

I have tried running Visual Studio in administrator mode, but that doesn't help. Is the only way to query Win32_Volume by building query strings and using ManagementObjectSearcher? Can't I do it using LINQ-style extension methods as can be done with Win32_LogicalDisk?


问题是您必须使用WQL查询来获取对象,这就是我使用的在我的测试中。



你提供的第一个例子是有效的,因为在Win32_LogicalDisk类中,DeviceID属性是一个Key。请参阅文档此处 [ ^ ]。



在Win32_Volume类中,DriveLetter不是Key字段,因此您无法使用路径语法重新使用创建ManagementObject来获取它。您必须使用WQL查询而不是ManagementObjectSearcher。
The problem is you have to use a WQL query to get at the object and that's what I used in my tests.

The first example you provided works because, on the Win32_LogicalDisk class, the DeviceID property is a Key. See the documentation here[^].

On the Win32_Volume class, DriveLetter is not a Key field so you cannot use the path syntax that you're using to create the ManagementObject to get at it. You must use a WQL query instead with the ManagementObjectSearcher.


这篇关于C#中的WMI问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆