HOW TO READ ALL FILES INSIDE A SPECIFIC FOLDER

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
C#  .NET 
C#
 389  View(s)
Ratings:
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.

  1. Anupam Mishra

    Post:135

    Points:949
    Re: How to read all files inside a specific folder


    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!