Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to check if ManagementObject mo2 is null in this method:

C#
static string[] GetUsbcomDevices()
      {
          ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity");
          List<string> list = searcher2.Get().Cast<ManagementObject>().Select(mo2 => mo2["Name"].ToString()).Where(name => name.Contains("(COM")).ToList();
          string[] usbDevices = list.Distinct().OrderBy(s => s).ToArray();
          return usbDevices;
      }


Thanks,
Groover
Posted
Updated 23-Jun-20 4:17am
Comments
GrooverFromHolland 29-Dec-15 15:08pm    
With the help of OriginalGriff I came to a working solution that I will post so others can profit,

Groover

If the searcher is containing nulls, then you can ignore them quite easily:
C#
List<string> list = searcher2.Get().Where(m => m != null).Cast<ManagementObject>().Select(mo2 => mo2["Name"].ToString()).Where(name => name.Contains("(COM")).ToList();

But you shouldn't need to cast the collection to ManagementObjects, since the Get method returns a collection of them already: MSDN[^]
 
Share this answer
 
Comments
GrooverFromHolland 29-Dec-15 11:07am    
If I check for null after searcher2.Get(), than I get an error: ManagementObjectCollection does not contain a definition for Where.
If I check for null after Cast<<managementobject>>() mo2 still is null.

Groover
OriginalGriff 29-Dec-15 11:33am    
Sorry, you're right: ManagementObjectCollection implements IEnumeable, but not the generic version you need for Linq Methods. You need the Cast to get it to a generic collection.

But if it's getting through the Cast, it isn't null anyway ( you would get an "invalid cast" exception if it were).
Which means it isn't "mo2" that is null, it's the "Name" index that returns null.
You can prove this very easily:
...Cast<managementobject>().Select(mo2 => (mo2["Name"] ?? "").ToString()).Where(...
Will remove the exception - and probably all of the items!

And a quick check on MSDN:
https://msdn.microsoft.com/en-us/library/system.management.managementobject_properties%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Says that there is no "Name" property for a ManagementObject, or for it's base classes.
GrooverFromHolland 29-Dec-15 13:06pm    
You are absolutely right, I will try a different approach to get a list of USB-Serial devices.

Thank You for Your effort,

Groover
OriginalGriff 29-Dec-15 14:03pm    
You're welcome!
this is the solution:
C#
static string[] GetUsbcomDevices()
       {
           List<ManagementObject> listObj = new List<ManagementObject>();
           List<string> usbSerial = new List<string>();
           try
           {
               string query = "SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0";
               ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
               listObj = searcher.Get().Cast<ManagementObject>().ToList();
               searcher.Dispose();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
               usbSerial = new List<string>();
           }
           foreach (ManagementObject obj in listObj)
           {
               var targetObj = obj["Caption"];
               if (targetObj != null)
               {
                   string caption = targetObj.ToString();
                   if (caption.Contains("(COM"))
                   {
                       string name = caption.Substring(caption.LastIndexOf("(COM")).
                                                       Replace("(", string.Empty).
                                                       Replace(")", string.Empty);
                       usbSerial.Add(name);
                   }
               }
           }
           string[] usbDevices = usbSerial.Distinct().OrderBy(s => s).ToArray();
           return usbDevices;
       }


Groover
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900