18

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.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
kaka mishoo
  • 231
  • 1
  • 2
  • 9
  • @Andrei, for that the OP has to use `DirectoryInfo` class, currently `Directory.GetDirectories` will return a `string[]`, which will consist of full path. – Habib Oct 14 '14 at 15:38

8 Answers8

24

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.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
7

There is a simple one-liner:

Directory.GetDirectories(PathToFolder).Select(d => new DirectoryInfo(d).Name);
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.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

How about we use little linq:

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

piece of cake :D

Mohammad
  • 2,724
  • 6
  • 29
  • 55
1

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);
    }
}
Brian Deragon
  • 2,929
  • 24
  • 44
0

but its not get Directory Name. Ex: "NewFolder1", get: "D:\aaa\bbbb\cccc\dddd\NewFolder1".

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:\aaa\bbbb\cccc\dddd\NewFolder1";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
string nameOfDirOnly = dirInfo.Name; // NewFolder1

ListBox1.Items.Add(nameOfDirOnly);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

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;
    }
JWP
  • 6,672
  • 3
  • 50
  • 74
  • [`Parallel.ForEach` is not always faster](http://stackoverflow.com/a/6036611/284240). You should also mention why this solves OP's issue. – Tim Schmelter Oct 14 '14 at 15:51
  • Ya point well taken. However, when I design anything I always thing about putting something into re-usable libraries, the pattern above is good, but I do admit it's much better when there are thousands of directories. I answered one just yesterday where OP cut down processing by 4 times using this pattern. – JWP Oct 14 '14 at 16:01
  • Also, I'm a LINQ fan too. I try to write these types of answers to introduce folks to LINQ because, well, it's great! – JWP Oct 14 '14 at 16:02
0

This works for me as well:

var directoryName = Path.GetFileName(directoryPath);
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57