-
[2008] Convert code
I need to convert the following code to work in a visual studio 2008 windows form application, can anyone help? At the moment ObjectQuery, ManagementObjectSearcher, ManagementObject and ManagementObjectCollection are not valid.
Code:
Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL")
Dim searcher As New ManagementObjectSearcher(Scope, objectQuery)
Dim os As ManagementObject
Dim moColl As ManagementObjectCollection = searcher.Get()
Dim _list As String = ""
For Each os In moColl
Console.WriteLine(os("NetConnectionId"))
Next os
-
Re: [2008] Convert code
You'll have to import the System.Management namespace, or specify the full class path for those 4 classes.
-
Re: [2008] Convert code
For future reference, open the MSDN Library to the index and type in the class name, then click the class overview topic. It will give you the assembly (DLL) the class is defined in and the namespace it's a member of. You then just make sure you've referenced the assembly and imported the namespace. This will work for any and every type that in the .NET Framework.
-
Re: [2008] Convert code
I have added:
Imports System.Management
But it is still telling me that the types are not defined?
I have also loaded this example code from Microsoft help files into a new class and get the same error:
Code:
Imports System
Imports System.Management
Public Class Class1
Public Overloads Shared Function Main(ByVal args() As String) As Integer
Dim p As New ManagementPath("Win32_Service.Name=""Alerter""")
Dim o As New ManagementObject(p)
'Now it can be used
Console.WriteLine(o("Name"))
Return 0
End Function
End Class
ManagementPath and ManagementObject are not defined?
-
Re: [2008] Convert code
You may need to add a reference to system.management as JMC mentioned in his post.