C# 如何检查WMI查询是否返回0行?

C# 如何检查WMI查询是否返回0行?,c#,wmi,C#,Wmi,我正在编写一个显然是错误的C代码。 我正在尝试使用WMI查询获取pendrive数据,在继续操作后,检查查询是否返回0行以避免错误 ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_USBDevice"); ManagementObjectCollection drive = searcher.Get(); if (drive == null)

我正在编写一个显然是错误的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数据的正确方法吗?

使用:

让它万无一失:

if (drive==null || 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();
}