views:

81

answers:

5

I want to list the "sub-path" of files and folders for the giving folder (path)

let's say I have the folder C:\files\folder1\subfolder1\file.txt

if I give the function c:\files\folder1\

I will get subfolder1 subfolder1\file.txt

+1  A: 

Use the System.IO.Directory class and its methods

NullUserException
+1  A: 

You can use the Directory.GetFiles method to list all files in a folder:

string[] files = Directory.GetFiles(@"c:\files\folder1\", 
    "*.*",
    SearchOption.AllDirectories);

foreach (var file in files)
{
    Console.WriteLine(file);
}

Note that the SearchOption parameter can be used to control whether the search is recursive (SearchOption.AllDirectories) or not (SearchOption.TopDirectoryOnly).

0xA3
+3  A: 

Try something like this:

static void Main(string[] args)
{
    DirSearch(@"c:\temp");
    Console.ReadKey();
}

static void DirSearch(string dir)
{
    try
    {
        foreach (string f in Directory.GetFiles(dir))
            Console.WriteLine(f);
        foreach (string d in Directory.GetDirectories(dir))
        {
            Console.WriteLine(d);
            DirSearch(d);
        }

    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
RedFilter
+1  A: 
String[] subDirectories;
String[] subFiles;
subDirectories = System.IO.Directory.GetDirectories("your path here");
subFiles = System.IO.Directory.GetFiles("your path here");
tonythewest
A: 

I remember solving a similar problem not too long ago on SO, albeit it was in VB. Here's the question.

Alex Essilfie