Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Directories name

Tags:

c#

asp.net

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.

like image 629
kaka mishoo Avatar asked Oct 14 '14 15:10

kaka mishoo


People also ask

How do I see directory names in Linux?

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.

How are directories named?

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.


1 Answers

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.

like image 187
Christian Phillips Avatar answered Oct 18 '22 04:10

Christian Phillips