我正在编写明显错误的 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();
}
关于c# - 如何检查 WMI 查询是否返回 0 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015621/