Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if WMI Query returned 0 rows?

Tags:

c#

wmi

I'm working on a C# code that is obviously wrong. I'm trying to get a pendrive data with WMI query and after continuing with the operations, check if the query returned 0 rows to avoid bugs.

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();
}

Obviously the drive == null method does not work. How can I check it the proper way? And also, is this the proper way of getting a pendrive data?

like image 787
Wiktor Zawierucha Avatar asked Mar 12 '23 22:03

Wiktor Zawierucha


1 Answers

Make it foolproof:

if (drive==null || drive.Count == 0))
{
   MessageBox.Show("Failed to read data.");
   Application.Exit();
}
like image 192
Pankaj Kapare Avatar answered Mar 23 '23 02:03

Pankaj Kapare