Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
New to coding and i need help getting just the name of the folders in one directory. I can get the listing of the folders but it shows the full path and all i need is just the folder name.

example.

I can get this :

C:\Program Files (x86)\Adobe

but all i want is :

ADOBE

I need to get these items to show up in a list view. Help would be great. please no forwards to another site for information.
Posted

Since .NET 4.0 we have the DirectoryInfo class[^] with the method EnumerateDirectories and EnumerateDirectories(path).

Example from MSDN:
C#
// Create a DirectoryInfo of the Program Files directory.
DirectoryInfo dirPrograms = new DirectoryInfo(@"c:\program files");

DateTime StartOf2009 = new DateTime(2009, 01, 01);

// LINQ query for all directories created before 2009.
var dirs = from dir in dirPrograms.EnumerateDirectories()
            where dir.CreationTimeUtc < StartOf2009
            select new
            {
                ProgDir = dir,
            };
// Show results.
foreach (var di in dirs)
{
    Console.WriteLine("{0}", di.ProgDir.Name);
}

Regards,

Manfred
 
Share this answer
 
Comments
bbirajdar 3-Feb-12 8:23am    
Thank you Manfred for adding to my knowledge.. My +5
Manfred Rudolf Bihy 3-Feb-12 8:42am    
Your are welcome, come back any time! :)
Wendelius 3-Feb-12 15:11pm    
Good answer, 5.
Split the full directory name using '\' as split character and get the last value in the string array. That will be your folder name

C#
string [] folders=myfullPath.Split('\');
string finalFolder=folders[folders.Length-1];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900