Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  WMI connections made easy in VB.NET  »  Walking Through The Sample
WMI connections made easy in VB.NET
by Martin de Klerk | Published  04/24/2005 | .NET Framework | Rating:
Walking Through The Sample

The next part shows the sample code that is included in the ConnectionTester source. Just remove the module 'Sample' to incorporate the ConnectionTester class in your own project. As it is heavily commented, I won't insult the readers intelligence by explaining what's being explained:

   Dim WmiConn As New ConnectionTester
      '* point the ConnectionTester to the target computer.
      '* That can be done either by supplying the NetBios-name or
      '* Domain-name of the (remote) computer:
      WmiConn.ServerName = "localhost"
      '* OR by supplying the IP address of the (remote) computer
      '* computer as a string like:
      '*    WmiConn.IPAddress = ""
      '* OR by providing the HTTP address of the (remote) computer:
      '*    WmiConn.ServerName = ""
      '* The ConnectionTester's WMI capabilities are enabled per default.
      '* To disable them and only perform a quick online check, set the
      '* next value to False
      WmiConn.WmiCheck = True
      '* Test the connection
      Console.WriteLine("Connecting to {0}", wmiConn.ServerName)
      '* Check if target is online
      If wmiConn.IsOnLine = False Then
         Console.WriteLine("{0} is off-line.", WmiConn.ServerName)
         Exit Sub
         '* Display on-line state
         Console.WriteLine("{0} is online with IP address: {1}.", _
           WmiConn.ServerName, WmiConn.IPAddress)
      End If
      '* Check if errors occured while connecting
      If WmiConn.HasErrors = True Then
         Console.WriteLine("Error while connecting to {0}: {1}", _
           WmiConn.ServerName, WmiConn.ErrorMessage)
         Exit Sub
      End If
      '* Check if WMI connection to remote is active
      If WmiConn.WmiEnabled = False Then
         Console.WriteLine("Could not connect WMI with \{0}{1} ", _
           WmiConn.ServerName, WmiConn.WmiNamespace)
         '* Display the connection to WMI namespace
         Console.WriteLine("WMI connection with {0} established: {1}", _
           WmiConn.ServerName, WmiConn.wmiNameSpace)
         '* show operating system version running the (remote) WMI server
         Console.WriteLine("O.S. : {0} ", wmiconn.OperatingSystem)
         '* get all MSI installed programs (can take a while)
         Console.WriteLine("Retrieving information. Please wait......")
         '* Create storage for the query results
         Dim moc As System.Management.ManagementObjectCollection
         '* Instruct the (remote) WMI server to execute query
         moc = WmiConn.ExecWmiQuery("Select * From Win32_Product")
         '* when found, display program-names
         If Not moc Is Nothing Then
            Console.WriteLine(" - Programs installed on {0} -", _
            Dim mo As System.Management.ManagementObject
            For Each mo In moc
               Console.WriteLine( mo.GetPropertyValue("Caption"))
            Console.WriteLine(" - End of list -")
         End If
      End If
You can download the demonstration copy of the Connection Tester solution from the next page.
Sponsored Links