Get files and folders from a directory

The following code snippet shows how we can find array of file names with the help of Directory.GetFiles method.

 string[] filePaths = Directory.GetFiles(@"D:\Client\");
        for (int i = 0; i < filePaths.Length; i++)
        {
            Response.Write(filePaths[i].ToString()); //--->Print list of files in current directory.
        }

if you want to Get files from directory with its all sub directories then we add a new SearchOption parameter SearchOption.AllDirectories, see this:

 string[] filePaths1 = Directory.GetFiles(@"D:\Client\", "*.*", SearchOption.AllDirectories);
        for (int i = 0; i < filePaths1.Length; i++)
        {
            Response.Write(filePaths1[i].ToString());
        }

And if you want to get folders from directory, then you can use Directory.GetDirectories method:

 
 string[] filePaths3 = System.IO.Directory.GetDirectories("D:\\Client\\");       
        for (int i = 0; i < filePaths3.Length; i++)
        {
            Response.Write(filePaths3[i].ToString());
        }

Author: Pavan

I am asp.net developer have good knowledge of Asp.net ver 05,08,10 and good hand in sql server.Proficient in Object Oriented Programming and javascript, jQuery. Achievements - Integrate Spotfire, appnexus API ASP.net with sql server. Hobbies - Blogging ,Games, Movies ,Teaching,Keeping myself update with new technologies