5

I have the following code on the code behind file for a .aspx page in a project:

Dim searcher As New ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" & sFilePath.Substring(0, 2) & "'")

    For Each managementObject As ManagementObject In searcher.[Get]()
        Dim sRemoteName As String = TryCast(managementObject("RemoteName"), String)
        sRemoteName += sFilePath.Substring(2)
        Return (New Uri(sRemoteName)).ToString()
    Next

    Return sFilePath

The ManagementObjectSearcher and the ManagementObject are both underlined and it is telling me that they are not defined.

I have added the System.Management reference, removed and readded, deleted my cache, rebuilt the whole .aspx page, removed the .dll and numerous other troubleshooting advise I have found on google but still cannot find the answer to this problem.

Please help!

AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
user1001995
  • 51
  • 1
  • 1
  • 2

5 Answers5

12

At the top of the project add in the namespace as you would normally:

Imports System.Management

Then under the project menu at the top (in Visual Studio) select "Add Reference...". Under the ".Net" tab scroll down to "System.Management". Select that line and click OK.

scrawny
  • 141
  • 1
  • 7
  • 1
    The problem I was having was that my import statement wasn't working which was fixed by adding the reference manually. I wonder why the reference isn't added automatically by visual studio when I type in an import. – Aeropher May 07 '15 at 10:06
5

In Visual Studio > Add Reference > Assemblies > select System.Management. this way it will resolve ManagementObjectSearcher and ManagementObjects.

Anurag Gawande
  • 116
  • 2
  • 10
1

The version of Visual Studio that I have does not import ManagementObjectSearcher by importing "System.Management" namespace. If you have the same issue, try adding a reference to "System.Management.dll' by doing the following steps.

  1. Click on project properties on solution explorer in Visual Studio.
  2. Go to "References".
  3. Click on "Add" to add a new reference.
  4. Click on "Browse...".
  5. Navigate to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727".
  6. Add a reference to "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll".
Meisam Rasouli
  • 301
  • 2
  • 5
1

Add Imports System.Management to the top of the .vb file to allow you to use the class without specifying its namespace.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • when I add Imports System.Management to the top I get an error: "Namespace or type specified in the Imports 'System.Management' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one publlic member. Make sure the imported element name doesn't use any aliases." Not sure what I am supposed to do with that... – user1001995 Oct 18 '11 at 21:22
  • Then you haven't actually referenced the assembly. – SLaks Oct 18 '11 at 21:22
  • Thanks for your help on this, how do I reference the assembly? – user1001995 Oct 18 '11 at 21:24
  • Right-click the project and click Add Reference. – SLaks Oct 18 '11 at 21:26
  • I have added and removed using that process several times, I am missing something but cannot figure it out. – user1001995 Oct 18 '11 at 21:30
0

Before you add the "Imports" to your Class, you have first to add a Reference (to the DLL) to your Project.

Aten
  • 49
  • 2