Detecting taken Drive Letters

jack Lindsay

Member
Joined
Nov 27, 2008
Messages
13
Programming Experience
Beginner
Hello All,

I have made a little app which map's a drive copy's a directory and a couple of other things

for the mapping a drive a have a dropdownbox with all the letters of the alphabet, and a couple of txtfields for the remote name and share name.

i was wondering if it was possible to make the dropdown show all the available drive letters (because the likes of the C drive and others are already taken.

kind regards

Jack Lindsay
 
You can use the Environment.GetLogicalDrives, which returns a string array of the letters in use in the format of <Letter>:\, meaning {"A:\", "B:\", "C:\", etc} so it would be a matter of removing the taken ones from a list of all the letters in the alphabet.

Environment.GetLogicalDrives Method (System)
 
hi,

i went to that link you provided and tried there sample script in a new console application. the results only appear for a microsecond. is there anyway i can make the results appear in a label.
im fairly novice at vb, so if u could provide instructions i'd be very thankful
 
Sure, the Environment class exists in the framework and is accessible in WinForms, WPF, and console apps.

Here's how you could display the info in a listbox on a form in a winforms project, drop a listbox on the form and put this code into the form's load event:
VB.NET:
Listbox1.Items.AddRange(Environment.GetLogicalDrives())
 
ok thanks, so how would i now make it show all the available drive letters?
Well you know how to get a list (array) of taken letters and you know the letters in the alphabet (can make an array), how would you go about removing letters from your alphabet list?

I'm trying to get you to think about this on your own, hopefully you'll see the value in that.
 
some kind of if statement maybe?
A few actually, first off do you want the colon and/or the backslash to be included in the end result? The Environment.GetLogicalDrives returns an array with the ":\" included. What I would do is make a List(Of String) and fill it with the letters of the alphabet then loop through the Environment.GetLogicalDrives array (You can use the .UpperBounds(0I) on it to get the upper index if you use a For Loop) and inside the loop simply remove the letter from the array from the list and the end result is a list of just the available letters.

If you don't want the colon and the backslash then use .Replace(":\", String.Empty) when removing the item from the List.
 
I would just use the drive letters without the drive : and \ folder signs, therefore go for a List(Of Char). Something like this:
VB.NET:
Dim letters As New List(Of Char)("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
For Each drive As String In Environment.GetLogicalDrives
    letters.Remove(drive(0))
Next
Me.AvailComboBox.DataSource = letters
 
as it stands, my list box is letters only, with a string which puts the :\ in it, so if by end result you mean "available drive letters", then yes...having the :\ in the result will help.
so how do i go about doing these loops. i've got a comboBox, which i have loaded with the alphabet, but i dont know what you mean by loops
 
as it stands, my list box is letters only, with a string which puts the :\ in it, so if by end result you mean "available drive letters", then yes...having the :\ in the result will help.
so how do i go about doing these loops. i've got a comboBox, which i have loaded with the alphabet, but i dont know what you mean by loops
Since you want the end result to have the ":\" included John's code wouldn't work here.
VB.NET:
Dim LettersList As New List(Of String)
LettersList.AddRange(New String() {"A:\", "B:\", "C:\", "D:\", "E:\", "F:\", "G:\", "H:\", "I:\", "J:\", "K:\", "L:\", "M:\", "N:\", "O:\", "P:\", "Q:\", "R:\", "S:\", "T:\", "U:\", "V:\", "W:\", "X:\", "Y:\", "Z:\"})

For Each DrvLtr As String In Environment.GetLogicalDrives()
  LettersList.Remove(DrvLtr)
Next

DriveLettersComboBox.Items.AddRange(LettersList.ToArray)
 
If I needed the ":\" I would just convert the list like this in VB2008:
VB.NET:
Dim drives As List(Of String) = letters.ConvertAll(Function(letter) letter & ":\")
VB2005 doesn't do lambda expressions so you would have to add that Converter function:
VB.NET:
Function FormatDriveLetter(ByVal letter As Char) As String
    Return letter & ":\"
End Function
VB.NET:
drives = letters.ConvertAll(AddressOf FormatDriveLetter)
 
Back
Top