I want to find all directories in one directory in vb.net.
I found one script:
For Each Dir As String In Directory.GetDirectories(FolderName)
Com
For Each Dir As String In Directory.GetDirectories(FolderName)
ComboBox3.Items.Add(IO.Path.GetFileName(Dir))
Next
Use this to get only the directory name:
dirName = IO.Path.GetDirectoryName(fullPath)
The simplest way is to use System.IO.DirectoryInfo
:
For Each Dir As String In System.IO.Directory.GetDirectories(FolderName)
Dim dirInfo As New System.IO.DirectoryInfo(Dir)
ComboBox3.Items.Add(dirInfo.Name)
Next
(Obviously, you could parse it manually and extract out the text following the final '\', but I believe that the above is more readable)
Give this a try
For Each d As String In IO.Directory.GetDirectories(FolderName)
'IO.Path.GetFileName
'The characters after the last directory character in path.
ComboBox3.Items.Add(IO.Path.GetFileName(d))
Next
This takes advantage of the fact that you have a list of directories and what IO.Path.GetFileName actually does.
I think the easiest way would be using String.Replace
to remove FolderName
from the beginning of the directory full name.
For Each Dir As String In System.IO.Directory.GetDirectories(FolderName)
ComboBox3.Items.Add(Dir.Replace(FolderName, String.Empty))
Next