Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i get the name of a folder subdirectory. If i have a folder called candidates with 2 subdirectories how do i get the names of those 2 subdirectories.
Posted

Try below code. Specify path of your folder "candidates" to "direcoryPath" variable.

string direcoryPath = @"c:\candidates";

string[] subDirectories= System.IO.Directory.GetDirectories(direcoryPath);
 
Share this answer
 
v2
This should do:

C#
class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo directory = new DirectoryInfo(@"C:\MyDir");
        DirectoryInfo[] directories = directory.GetDirectories();

        foreach (DirectoryInfo dir in directories)
        {
            Console.WriteLine(dir.Name);
        }
    }
}


Take care of exception handling.

Cheers
 
Share this answer
 
Comments
Prasanta_Prince 15-Jul-11 22:31pm    
Good one.
Boss its very easy, use System.IO in in your program and then check the following link to explore Directory and Subdirectory related classes. I had a sample which is not there with me right the moment. Anyway checking the following link and subsequent sample programs will resolve your issue. Happy Coding

http://msdn.microsoft.com/en-us/library/system.io.aspx[^]
 
Share this answer
 
Comments
capdevillia 14-Jul-11 7:10am    
Kale sebo as in yes sir.
Mr.Sourav.Maitra 14-Jul-11 9:03am    
Means? What?
Mario's solution will work fine. But it you would like to do Deep look up then consider following,
C#
public class DirectoryLookup
{
    public void DeepLookup(string dName)
    {
        DirectoryInfo directory = new DirectoryInfo(dName);
        DirectoryInfo[] directories = directory.GetDirectories();
        foreach (DirectoryInfo dir in directories)
        {
            DeepLookup(dir.FullName);
            Console.WriteLine(dir.FullName);
        }
    }
}

Hope it helps. :)
 
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