Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - WMI Programming with Visual Basic.NET: Bre...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

WMI Programming with Visual Basic.NET: Breaking the Ice
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2005-06-21

    Table of Contents:
  • WMI Programming with Visual Basic.NET: Breaking the Ice
  • Our first program to connect to WMI (on a local computer)
  • Getting information from WMI
  • Our first program to connect to WMI (on a remote computer)

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!

    WMI Programming with Visual Basic.NET: Breaking the Ice - Getting information from WMI


    (Page 3 of 4 )

    In order to get information (or data) from WMI, either on the local computer or from a remote computer, you must connect to the WMI service by connecting to a specific namespace. For example, the "root\cimv2" namespace (which is also the default) includes most of the classes that represent resources commonly associated with a computer and operating system. "root" is the main namespace to start with WMI. "cimv2" is a sub namespace available within the "root". There exist literally hundreds (or even thousands) of classes in the "root\cmv2" namespace. Similarly, there exist several other namespaces as well, logically grouped based on specific tasks.

    The above paragraph provides you with just a procedure for working with WMI. But how do you implement it? The version of WMI that shipped with Windows 2000/2003 displayed classic COM interfaces to client applications. Therefore, you can access WMI-instrumented management data and events via the .NET COM Interop layer.

    But there is a much better way to work with WMI from .NET. The System.Management namespace, defined as part of the .NET Framework, provides an easy-to-use, intuitive API for consuming WMI data and events. A lot of the complexities and idiosyncrasies of WMI APIs are taken care of by System.Management, and its class definitions follow the standard design paradigm of the .NET Framework.

    Now, I would like to take a moment to explain the program in the previous section. Let us consider the following statement.

    Imports System.Management

    The above statement straight away imports all the necessary ingredients to work with WMI in our application. It must be added as a reference to our application before importing it.

    Dim diskClass As New ManagementClass("Win32_LogicalDisk")

    The above statement connects to a WMI class called "Win32_LogicalDisk". WMI creates a separate instance of this class for each and every logical drive present in the system. The class exists in "root\cimv2" namespace, which is the default namespace. You need not specify "root\cimv2" namespace, if you are working with any class within that namespace. If you really would like to do so, you need to change the above statement to something like the following:

    Dim diskClass As New ManagementClass ("\\.\root\cimv2:Win32_LogicalDisk")

    It is something like the UNC (Universal Naming Convention). The following gives you syntax for using the convention:

    \\<ServerName>\root\<subnamespace>:<class>

    Here is the next statement in the program:

    Dim disks As ManagementObjectCollection = diskClass.GetInstances()

    The above statement returns all the "instances" (or drives) of drives logically present in your system. Each drive returned (as part of the collection) is an "instance" of the class "Win32_LogicalDisk".

    For Each disk In disks
      arDisks.Add(disk("deviceid").ToString())
    Next disk

    The above loop iterates through each instance of drives (present in the collection) and adds the "deviceid" to the arraylist. "deviceid" is a public member of the class "Win32_LogicalDisk". As we received the instances of the class "Win32_LogicalDisk" (from WMI), we are retrieving the value available in the "deviceid" member of each of those instances using a loop.

    Me.lstDrives.Items.AddRange(arDisks.ToArray)

    Finally, we add all the elements available in the arraylist to the listbox.

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · nice article. just what i wanted
     

    VISUAL BASIC.NET ARTICLES

    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway