Learn how to a build a cloud-first strategyRegister Now

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

VB.NET - Listing mapped drives on a remote PC

I had written some code in VB.NET/Visual Studio 2012 but since then I lost all data on the hard drive and consequently lost the source code.
I have used .NET Reflector to get back most of it but I am puzzled by some entries which don't make any sense to me as I can't remember the code I used and it has changed it slightly:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        Me.ListView1.Items.Clear()
        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
        Main.WMICall(Main.txtHostName.Text)
        If Main.WMICall(Main.txtHostName.Text) Then
            Dim scope As New ManagementScope(("\\" & Main.txtHostName.Text & "\root\cimv2"))
            scope.Connect()
            Try
                Dim enumerator As ManagementObject
                Dim query As New ObjectQuery("Select * from Win32_MappedLogicalDisk")
                Dim objects As ManagementObjectCollection = New ManagementObjectSearcher(scope, query).Get
                Try
                    enumerator = objects.GetEnumerator
                    Do While enumerator.MoveNext
                        Dim current As ManagementObject = DirectCast(enumerator.Current, ManagementObject)
                        Dim items As String() = New String(3 - 1) {}
                        items(0) = ToString(current.Item("Name"))
                        items(1) = ToString(current.Item("ProviderName"))
                        Dim item As New ListViewItem(items)
                        Me.ListView1.Items.Add(item)
                    Loop
                Finally
                    If (Not enumerator Is Nothing) Then
                        enumerator.Dispose()
                    End If
                End Try
            Catch ex As Exception

            End Try
        End If
        Me.Cursor = Cursors.Default

    End Sub

Open in new window

It's this part that isn't recognised:
enumerator = objects.GetEnumerator
                    Do While enumerator.MoveNext
                        Dim current As ManagementObject = DirectCast(enumerator.Current, ManagementObject)

I have no idea what was there originally. Any help?
0
fruitloopy
Asked:
fruitloopy
1 Solution
 
Fernando SotoCommented:
Hi fruitloopy;

The original code may be more closer to the code snippet below.

Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
    Me.ListView1.Items.Clear()
    Me.Cursor = Cursors.WaitCursor
    Application.DoEvents()
    Main.WMICall(Main.txtHostName.Text)
    If Main.WMICall(Main.txtHostName.Text) Then
        Dim scope As New ManagementScope(("\\" & Main.txtHostName.Text & "\root\cimv2"))
        scope.Connect()
        Dim query As New ObjectQuery("Select * from Win32_MappedLogicalDisk")
        Dim objects As ManagementObjectCollection = New ManagementObjectSearcher(scope, query).Get
        
        For Each mObject As ManagementObject In objects
            Dim items As String() = New String(2) {}
            items(0) = mObject.Item("Name")
            items(1) = mObject.Item("ProviderName")
            Dim item As New ListViewItem(items)
            Me.ListView1.Items.Add(item)
        Loop
    End If
    Me.Cursor = Cursors.Default                               
End Sub 

Open in new window

0
 
fruitloopyAuthor Commented:
Close enough and it certainly gave me the will to carry on!
It's this code that got the job done:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        ListView1.Items.Clear()
        Cursor = Cursors.WaitCursor
        Application.DoEvents()
        Main.WMICall(Main.txtHostName.Text)
        
        Try
            If Main.WMICall(Main.txtHostName.Text) Then
                Dim scope As New ManagementScope(("\\" & Main.txtHostName.Text & "\root\cimv2"))
                scope.Connect()
                Dim query As New ObjectQuery("Select * from Win32_MappedLogicalDisk")
                Dim objects As ManagementObjectCollection = New ManagementObjectSearcher(scope, query).Get
                Dim lvi As New ListViewItem
                Dim nX As Integer

                For Each mObject As ManagementObject In objects
                    nX += 1
                    lvi = ListView1.Items.Add(mObject("Name"))
                    lvi.SubItems.Add(mObject("ProviderName"))
                Next
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Me.Cursor = Cursors.Default

    End Sub

Open in new window

Thanks
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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