Home > DeveloperSection > Forums > How to read all files inside a specific folder
Anupam Mishra

Total Post:135

Points:949
Posted on    January-22-2016 6:35 AM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 206  View(s)
Rate this:
Hi everyone,
I want to access all  file name which is text files in my specific folder i.e. "d:/Anupam Mishra". i am trying to use following code but i can not get correct output. Please give me a solution.
Code:

         try
              {
               
foreach (string dir in Directory.GetDirectories("d:"))

                {
                   
foreach (string fileName in Directory.GetFiles("d:/Anupam Mishra"))

                    {
                       
Console.WriteLine(fileName );

                    }
                  }
              }
           
catch (System.Exception ex)
            {
               
Console.WriteLine(ex.Message);
            }



Thank You.



Anupam Mishra

Total Post:135

Points:949
Posted on    January-22-2016 7:04 AM


If we are using this code . it will working on above problem:


class Program
    {
        static void DirSearch()
        {
            try
            {
                foreach (string file in Directory.GetFiles("d:\\Anupam Mishra", "*.txt"))
                {
                    Console.WriteLine(file);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
        static void Main(string[] args)
        {
            DirSearch();
            Console.ReadKey();
        }
    }

Output: 
//retrieve all text files which is inside a specified directory.


Don't want to miss updates? Please click the below button!

Follow MindStick