Read Text File in C#

        public string ReadFile(string baseDirectory)
        {           
            System.IO.StreamReader file = new System.IO.StreamReader(FilePath);  //.txt file path   
            string strLogs = String.Empty;   
             
            //Option A: Read the file and display it line by line.
            int counter = 0;
            string line;
            while ((line = file.ReadLine()) != null)
            {
                 Console.WriteLine(line);
                 strLogs += line + Environment.NewLine;
                 counter++;
            }

            //Option B:
            //strLogs = file.ReadToEnd().Replace("\n", "<brk />"); //remove "k" from "brk"
  
            //Option C:
            //strLogs =System.IO.File.ReadAllText(FilePath);

            file.Close();
            return strLogs;
        } 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s