Link to home
Start Free TrialLog in
Avatar of fruitloopy
fruitloopy

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fruitloopy
fruitloopy

ASKER

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