如何检查 WMI 查询是否返回 0 行?
How to check if WMI Query returned 0 rows?
我正在处理明显错误的 C# 代码。
我正在尝试使用 WMI 查询获取 pendrive 数据,并在继续操作后检查查询是否返回 0 行以避免错误。
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_USBDevice");
ManagementObjectCollection drive = searcher.Get();
if (drive == null)
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}
显然drive == null
方法不起作用。我怎样才能以正确的方式检查它?
还有,这是获取 pendrive 数据的正确方法吗?
使用Count
:
if (drive.Count == 0)
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}
万无一失:
if (drive==null || drive.Count == 0))
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}
我正在处理明显错误的 C# 代码。 我正在尝试使用 WMI 查询获取 pendrive 数据,并在继续操作后检查查询是否返回 0 行以避免错误。
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_USBDevice");
ManagementObjectCollection drive = searcher.Get();
if (drive == null)
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}
显然drive == null
方法不起作用。我怎样才能以正确的方式检查它?
还有,这是获取 pendrive 数据的正确方法吗?
使用Count
:
if (drive.Count == 0)
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}
万无一失:
if (drive==null || drive.Count == 0))
{
MessageBox.Show("Failed to read data.");
Application.Exit();
}