I want to check if a certain feature is installed on a certain machine.
I have a powershell code that checks this, and now I want to check this from .net code.
I can see that in the cmdlet, the code checks if there is an invalid namespace
error.
When searching the web, I found the following code:
ManagementClass myClass = new ManagementClass(scope, path, getOptions);
try
{
myClass.get();
}
catch (System.Management.Exception ex)
{
if (ex.ErrorCode == ManagementStatus.InvalidNamespace)
{
return true;
}
}
...
I want to clean this code a bit, so basically I have 2 questions:
Is there another way to check for an
InvalidNamespace
error? (The code I've copied was later used to invoke some method withinmyClass
, so I wonder if I can somehow achieve my goal in a more direct way)Do I really need the parameter
getOptions
?
To get all the wmi namespaces, you must first connect to the root namespace and then query for all the __NAMESPACE instances, and for each instance recursively repeat this process. about the getOptions parameter which is a
ObjectGetOptions
class is not necessary in this case, so can be null.Check this code to get all the wmi namespaces (you can populate a list with that info and then check if the namespace exist in the machine)