[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Simple Question Using WMI in VB 2005 .Net

Hey Again Everyone,

I was looking at some of the discussion threads on EE regarding using WMI to obtain USB information.  Some of the threads seemed to have full solutions like the one I've pasted at the end of this question.  When I cut/paste this into VB 2005, it keeps telling me that the managementobjectsearch, managementobtect and managementobjectcollection is not defined.  Do I need to somehow "initialize" the managementobjects?

Chris, Baltimore

Function GetInfo(ByVal strServer As String)
   Dim strDetail As String
   Dim objWMISearch As ManagementObjectSearcher
   Dim colItems As ManagementObjectCollection
   Dim objItem As ManagementObject
   Dim objWMI As ManagementScope = New ManagementScope("\\" & strServer & "\root\cimv2")
   Dim strQuery As String = "Select * From Win32_OperatingSystem"
   Try
        objWMI.Connect()
        objWMISearch = New ManagementObjectSearcher(objWMI.Path.ToString, strQuery)
        colItems = objWMISearch.Get
        For Each objItem In colItems
                strDetail=objItem.GetPropertyValue("Name")
        Next
    Catch
        MessageBox.Show(Err.Description)
        strDetail=Nothing
    End Try
    Return strDetail
End Function
0
clebo99
Asked:
clebo99
  • 4
  • 3
2 Solutions
 
Jens FiedererCommented:
Did you use
Imports System.Management

?
0
 
clebo99Author Commented:
Nope...didn't know I had to do that...I just copied/pasted the code.  Is an "IMPORT" kind of like an INCLUDE?
0
 
Jens FiedererCommented:
A little.  The C# equivalent is using.

It allows you to use the given namespace without prefixes.  Since ManagementObjectSearcher is in the System.Management namespace, unless you have that "imports", you need to say
System.Management.ManagementObjectSearcher every time.

Of course, you ALSO need to have a reference in your project to the System.Management assembly.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
Priest04Commented:
Imports System.Management wont work until you add reference to System.Management.dll. To do this you need to click on Project - Add reference - and locate System.Management. Then you add Imports line as jensfiedererD said

So, you use objects from Management namespace, you need to add reference to it first. using Imports is optional and it means next:

1) if you dont use Imports System.Management then to use ManagementObjectSearcher you need to declare it with full name.

Dim objWMISearch As System.Management.ManagementObjectSearcher

2) If you put Imports System.Management on the top of your class then this is all you need

Dim objWMISearch As ManagementObjectSearcher

It makes coding lot easier

Goran
0
 
Jens FiedererCommented:
Which is why I wrote "you ALSO need to have a reference in your project to the System.Management assembly".

It is not clear from the sample provided that this reference did not already exist, but once it was clear the "imports" was not there, I figured that was worth checking as well.
0
 
Priest04Commented:
Well, as you can see, the time difference between ur and mine posts is less than 10 minutes, so there is a slight posibility that ur post didnt exist in the time I started writing mine, otherwise I surely wouldnt repeat what u said

Goran
0
 
Jens FiedererCommented:
Ah....that makes sense.  Sorry.
0
 
Priest04Commented:
No problem

Goran
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now