How to get Directories name

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:aaabbbbccccddddNewFolder1”.

I have only get Directory Name.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

There is a simple one-liner:

Directory.GetDirectories(PathToFolder).Select(d => new DirectoryInfo(d).Name);

Method 3

Currently you are using Directory.GetDirectories, It will return a string[] which will consist of full path for the directories. Instead use DirectoryInfo class, later you can use the property DirectoryInfo.Name to get only the name of the directories and not the full path like:

void DirSearch(string sDir)
{
    DirectoryInfo dirInfo = new DirectoryInfo(sDir);
    foreach (var d in dirInfo.GetDirectories("*", SearchOption.AllDirectories))
    {
        ListBox1.Items.Add(d.Name);
    }
}

It appears that you are trying to recursively search all the sub directories as well, you can use the SearchOption.AllDirectories in your code to include all the sub directories.

Method 4

How about we use little linq:

ListBox1.Items.AddRange(System.IO.Directory.GetDirectories(@"C:").Select(x => new DirectoryInfo(x).Name).ToArray());

piece of cake 😀

Method 5

If you don’t want to create DirectoryInfo or FileInfo classes which can sometimes cause errors and introduce additional overhead, you could do it this way instead:

void DirSearch(string sDir)
{
    foreach (var d in System.IO.Directory.GetDirectories(sDir))
    {
        ListBox1.Items.Add(d.TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar).Last());
        DirSearch(d);
    }
}

It’s also a hair bit faster, depending on how many and how large the directories you’re searching are…

If you don’t need it to be cross-platform/can always guarantee the file system has a slash separator, you can make it a little “shorter”/less verbose.

void DirSearch(string sDir)
{
    foreach (var d in System.IO.Directory.GetDirectories(sDir))
    {
        ListBox1.Items.Add(d.TrimEnd('\').Split('\').Last());
        DirSearch(d);
    }
}

Method 6

but its not get Directory Name. Ex: “NewFolder1”, get: “D:aaabbbbccccddddNewFolder1”.

GetDirectoryName returns the full path of the directory(it can be used also for files) but you want only the last part, so NewFolder1 in your example?

You can use DirectoryInfo.Name

string dir = @"D:aaabbbbccccddddNewFolder1";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
string nameOfDirOnly = dirInfo.Name; // NewFolder1

ListBox1.Items.Add(nameOfDirOnly);

Method 7

Here’s something that will work for you…

    public IEnumerable<string> GetDirInfoFast(string dir, string pattern)
    {
        //you should put try catch here....
        var di = new DirectoryInfo(dir);
        var list = new List<string>();
        //put in the pattern
        var dirs = di.GetDirectories(pattern);
        Parallel.ForEach(dirs, p => {
            list.Add(p.Name);
        });
        return list;
    }

Method 8

This works for me as well:

var directoryName = Path.GetFileName(directoryPath);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x