44558

check if WMI namespace exists from c#

Question:

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:

<ol><li>

Is there another way to check for an InvalidNamespace error? (The code I've copied was later used to invoke some method within myClass, so I wonder if I can somehow achieve my goal in a more direct way)

</li> <li>

Do I really need the parameter getOptions?

</li> </ol>

Answer1:

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 <a href="http://msdn.microsoft.com/en-us/library/system.management.objectgetoptions.aspx" rel="nofollow">ObjectGetOptions</a> 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)

using System; using System.Collections.Generic; using System.Text; using System.Management; namespace MyConsoleApplication { class Program { static private void GetWmiNameSpaces(string root) { try { ManagementClass nsClass = new ManagementClass( new ManagementScope(root), new ManagementPath("__namespace"), null); foreach (ManagementObject ns in nsClass.GetInstances()) { string namespaceName = root + "\\" + ns["Name"].ToString(); Console.WriteLine(namespaceName); //call the funcion recursively GetWmiNameSpaces(namespaceName); } } catch (ManagementException e) { Console.WriteLine(e.Message); } } static void Main(string[] args) { //set the initial root to search GetWmiNameSpaces("root"); Console.ReadKey(); } } }

Recommend

  • Paho Rabitmqq connection getting failed
  • check if WMI namespace exists from c#
  • How can I make ARR work with SSL offloading DISABLED? 502.3 Bad Gateway
  • File Download from Webview is not working in android, I am trying this way,
  • My WebView does not fill the tabs' screens (but fills the cellphones' screens with no pb)
  • Problems using javazoom's Player
  • Manually set Validation error on Silverlight control
  • Unix shell script to search for error codes in thousand files then print the count in text file
  • How to create Json object from String containing characters like ':' ,'[' and &#
  • Refused: not authorized error occurs with IBM IoT Foundation on Bluemix
  • How to share a database between ASP.NET MVC 5 application and .NET console application?
  • https in htaccess and order of rules (using Expression Engine)
  • Python equivalent of Scala's exists() function?
  • twisted.internet.error.ConnectError when run scrapy spider
  • Is there a equivalent to JSON.Net in Java? [duplicate]
  • Submission of new app with iAds
  • How can I print out a large table of symbols from MATLAB to form a cross-stitch chart?
  • end daemon processes with multiprocessing module
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • ImportError: cannot import name Pubnub
  • Most efficient way to move table rows from one table to another
  • Suppressing passwd when calling sqlplus from shell script
  • Debugging VB6 Code From Visual Studio 2010
  • Sequential (transactional) API calls in angular 4 with state management
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • print() is showing quotation marks in results
  • JQuery Internet Explorer and ajaxstop
  • Use of this Javascript
  • Allowing both email and username for authentication
  • How to delay loading a property with linq to sql external mapping?
  • Get one-time binding to work for ng-if
  • Javascript Callbacks with Object constructor
  • Running a C# exe file
  • How do you troubleshoot character encoding problems?
  • Matrix multiplication with MKL
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Django query for large number of relationships