LabRat
LabRat

Reputation: 2014

get all mapped network drives in dropdown list

Using VB.Net is it possible to list all the mapped network directories/ drives in a dropdown list?

I have goggled but cant find anything useful..

Upvotes: 9

Views: 9795

Answers (2)

Bruce Schaller
Bruce Schaller

Reputation: 21

Mike has a great answer, to which I'll add something to keep it from growing each time it's clicked open. Good for say....Combo boxes in VB.

    Dim drive As System.IO.DriveInfo

If DropDownList1.Count < 1

     For Each drive In System.IO.DriveInfo.GetDrives()

         If drive.DriveType = IO.DriveType.Network Then

             DropDownList1.Items.Add(drive.Name)

         End If
     Next
End If

Upvotes: 1

Michael Eakins
Michael Eakins

Reputation: 4179

To add it to a DropDownList:

 Private Sub TestCase1()
        Dim drive As System.IO.DriveInfo

    For Each drive In System.IO.DriveInfo.GetDrives()
        If drive.DriveType = IO.DriveType.Network Then
            DropDownList1.Items.Add(drive.Name)
        End If
    Next
End Sub

This is how I would do it in C#:

private void TestCase1()
    {

        //Recurse through the drives on this system and add them to the new DropDownList DropDownList1 if they are a network drive.
        foreach(System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
        {
            //This check ensures that drive is a network drive.
            if (drive.DriveType == System.IO.DriveType.Network)
            {
                //If the drive is a network drive we add it here to a combobox.
                DropDownList1.Items.Add(drive);
            }
        }
    }

Upvotes: 8

Related Questions