I use this code for get directories name:
void DirSearch(string sDir)
{
foreach (var d in System.IO.Directory.GetDirectories(sDir))
{
ListBox1.Items.Add(System.IO.Path.GetDirectoryName(d));
DirSearch(d);
}
}
but its not get Directory Name. Ex: "NewFolder1", get: "D:\aaa\bbbb\cccc\dddd\NewFolder1".
I have only get Directory Name.
Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.
The name of each directory must be unique within the directory where it is stored. This ensures that the directory has a unique path name in the file system. Directories follow the same naming conventions as files, as explained in Filename naming conventions.
This should work:
foreach (var d in System.IO.Directory.GetDirectories(@"C:\"))
{
var dir = new DirectoryInfo(d);
var dirName = dir.Name;
ListBox1.Items.Add(dirName);
}
Also, you could shortcut...
foreach (var d in System.IO.Directory.GetDirectories(@"C:\"))
{
var dirName = new DirectoryInfo(d).Name;
ListBox1.Items.Add(dirName);
}
I just used route of C for testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With