ManagementObjectCollection usage

Posted on 2009-04-28
Last Modified: 2013-11-27
I am trying to use the ManagementObjectCollection class to get some system information. However, the attcahed code snippet gives me the errors:
   "ManagementClass not defined"
   "ManagementObjectCollection not defined"
   "ManagementObjectCollection.ManagementObjectEnumerator" not defined.

From what I can see in MSDN, I have to import System and System.Management to get these classes.

I have both imports in my code but still get the errors.

I am using Visual Studio.Net (Visual Basic 2008) on a WinXP SP3, .Net 3,.5 PC.

Can someone please advise what I am missing.


Dim ManClass As ManagementClass = New ManagementClass("Win32_ComputerSystem")

Dim ManObj As ManagementObjectCollection = ManClass.GetInstances()

Dim ManOblCollection As ManagementObjectCollection.ManagementObjectEnumerator = ManObj .GetEnumerator

ManOblCollection .MoveNext()

s = ManOblCollection .Current.Properties("CurrentTimeZone").Value.ToString()

Open in new window

Question by:jindalee
    LVL 96

    Accepted Solution

    Try something like this:

    1) Add a reference to the System.Management.dll to your project.

    2) Add Imports System.Management at the top of the class module.

    3) Use a ManagementObjectSearcher.


    ' Add a reference to System.Management.dll.
    Imports System.Management
    Public Class Win32_ComputerSystem
    	Public AutomaticResetBootOption As Boolean
    	Public AutomaticResetCapability As Boolean
    	Public BootROMSupported As Boolean
    	Public CurrentTimeZone As Short
    	Public DaylightInEffect As Boolean
    	Public Domain As String
    	Public EnableDaylightSavingsTime As Boolean
    	Public InfraredSupported As Boolean
    	Public Manufacturer As String
    	Public Model As String
    	Public Name As String
    	Public NetworkServerModeEnabled As Boolean
    	Public NumberOfProcessors As Integer
    	Public PartOfDomain As Boolean
    	Public PauseAfterReset As Long
    	Public PowerManagementSupported As Boolean
    	Public SystemStartupDelay As Integer
    	Public SystemStartupSetting As Integer
    	Public SystemType As String
    	Public TotalPhysicalMemory As Long
    	Public UserName As String
    	Public WakeUpType As Integer
    	Public Workgroup As String
    	Public Shared ReadOnly Property Info() As Win32_ComputerSystem
    			Dim query As String = "Select * From Win32_ComputerSystem"
    			Dim searcher As New ManagementObjectSearcher(query)
    			Dim results As ManagementObjectCollection = searcher.Get()
    			Dim machine As New Win32_ComputerSystem
    			For Each entryCurrent As ManagementObject In results
    				machine.AutomaticResetBootOption = entryCurrent("AutomaticResetBootOption")
    				machine.AutomaticResetCapability = entryCurrent("AutomaticResetCapability")
    				machine.BootROMSupported = entryCurrent("BootROMSupported")
    				machine.CurrentTimeZone = entryCurrent("CurrentTimeZone")
    				machine.DaylightInEffect = entryCurrent("DaylightInEffect")
    				machine.Domain = entryCurrent("Domain")
    				machine.EnableDaylightSavingsTime = entryCurrent("EnableDaylightSavingsTime")
    				machine.InfraredSupported = entryCurrent("InfraredSupported")
    				machine.Manufacturer = entryCurrent("Manufacturer")
    				machine.Model = entryCurrent("Model")
    				machine.Name = entryCurrent("Name")
    				machine.NetworkServerModeEnabled = entryCurrent("NetworkServerModeEnabled")
    				machine.NumberOfProcessors = Convert.ToInt32(entryCurrent("NumberOfProcessors"))
    				machine.PartOfDomain = entryCurrent("PartOfDomain")
    				machine.PauseAfterReset = entryCurrent("PauseAfterReset")
    				machine.PowerManagementSupported = entryCurrent("PowerManagementSupported")
    				machine.SystemStartupDelay = Convert.ToInt32(entryCurrent("SystemStartupDelay"))
    				machine.SystemStartupSetting = entryCurrent("SystemStartupSetting")
    				machine.SystemType = entryCurrent("SystemType")
    				machine.TotalPhysicalMemory = Convert.ToInt64(entryCurrent("TotalPhysicalMemory"))
    				machine.UserName = entryCurrent("UserName")
    				machine.WakeUpType = Convert.ToInt32(entryCurrent("WakeUpType"))
    				machine.Workgroup = entryCurrent("Workgroup")
    				Exit For
    			Next entryCurrent
    			Return machine
    		End Get
    	End Property	 'Info
    End Class

    Open in new window

    LVL 1

    Author Comment

    I'm still learning. I have the following at the top of my code:

    IMports System
    IMports System.Management

    Is that differenmt from adding a reference?
    LVL 1

    Author Closing Comment

    Thank you. I found that I had to add theimports as references even though I had them at the top of the source file. Works a treat.
    LVL 96

    Expert Comment

    by:Bob Learned
    I didn't get a chance to respond.  Did you answer your own question about adding a reference?

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now