• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 979
  • Last Modified:

ManagementObjectCollection usage

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.

Thanks


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

0
jindalee
Asked:
jindalee
  • 2
  • 2
1 Solution
 
Bob LearnedCommented:
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.

Example:


' 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
		Get
			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

0
 
jindaleeAuthor Commented:
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?
0
 
jindaleeAuthor Commented:
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.
0
 
Bob LearnedCommented:
I didn't get a chance to respond.  Did you answer your own question about adding a reference?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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