Basically, stop using
var
unless you need to.
When you write this:
var objWmiService = Interaction.GetObject(@"Winmgmts:\\.\root\cimv2");
objWmiService
is type defined by context as the return type for
Interaction.GetObject
which - understandably - is an
object
because the return value could be
anything.
If you'd written it using a " proper" type name instead, you'd have got an error message which would have been a lot more specific:
MyActuialType objWmiService = Interaction.GetObject(@"Winmgmts:\\.\root\cimv2");
It would have told you that you need to cast the return value to your actual type before you can access any field, properties, or methods.
So use a "proper" type, and then use
as
to cast it, checking the casted value for
null
in case it isn't what you expected.