Best method

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
I have just created some code, mainly from the help, which gets all the directory names within a given directory.

This code uses the DIR function, which I think is the visual basic function not .NET?

Is there a better function to use?

Thnx
 
Directory class GetDirectories method returns a string array of the subdirectories.
e.g.
Code:
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
 
So where am I going wrong here? I am trying to obtain only the subdirectory name rather than the full path. All I get is blanks?

Code:
 Dim strDirectories As String() = Directory.GetDirectories(Application.StartupPath & "\photos\")

        Dim intCharCount, intIndex As Integer

        Dim strDirectory As String

        intIndex = 0

        For Each strDirectory In strDirectories

            intCharCount = strDirectories(intIndex).Length

            Me.cboSites.Items.Add(strDirectory.Substring(intCharCount))

            intIndex += 1

        Next
 
I have just seen my error :p

I am obtaining the entire length of the string.....plonker!
 
Code:
Dim strPath As String = Application.StartupPath & "\photos\"

        Dim strDirectories As String() = Directory.GetDirectories(strPath)

        Dim intCharCount As Integer

        Dim strDirectory As String

        For Each strDirectory In strDirectories

            intCharCount = strPath.Length

            Me.cboSites.Items.Add(strDirectory.Substring(intCharCount))

        Next

:D
 
Sorry, I hate unneccessary locals

Code:
  Dim strPath As String = Application.StartupPath & "\photos\"

        For Each strDirectory As String In System.IO.Directory.GetDirectories(strPath)
            Me.cboSites.Items.Add(strDirectory.Substring(intCharCount))
        Next
 
Though your example isnt too bad, it is good practice to keep your lines simple, so it is easier to decipher what your procedures are doing. :) (Also, if Im not mistaken, you cant put As String in the For Each Loop on .NET 2002.)
 
Back
Top