Directory list into listbox

Johnson

Well-known member
Joined
Mar 6, 2009
Messages
158
Programming Experience
Beginner
VB.NET:
        Dim sDirs() As String
        Dim i As Integer
        sDirs = System.IO.Directory.GetDirectories(txtPath.Text)
        For i = 0 To 5
            ListBox1.Items.Add(sDirs(i))
        Next

Lists the first 6 dirs it finds.

But how can i get the last listed dir into a varible so i can use its location?
 
Your only setting it to loop 6 times. Set it to loop the length of your array -1

VB.NET:
For intIndex as Integer = 0 to sDirs.Length -1
    Listbox1.Items.Add(sDirs(intIndex))
Next intIndex

'To get just the last entry as your question request
VB.NET:
Dim strLastDir As String = ""
strLastDir = sDirs(sDirs.Length -1)
 
Hi tom. but thats not what i wanted. I know i am only looping 6 times.

strLastDir = sDirs(sDirs.Length - 1) will always return the last dir listed in the directory. c:\WINDOWS in this case

For i = 0 To 5 was only for an example to save me writing out the random number function.

So this still doesnt answer my question on how to put the last listboix entry into a varible

thanks
 
The last listbox item is the same as getting the last item from an array, use the ListBox.Items.Count -1 to get the last index in the listbox.
 
Back
Top