C# Program to Read Lines from a File until the End of File is Reached

This is a C# Program to read lines from a file until the end of file is reached.

Problem Description

This C# Program Reads Lines from a File until the End of File is Reached.

Problem Solution

Here StreamWriter writes text files and this program read lines from a file that is created.

Program/Source Code

Here is source code of the C# Program to Read Lines from a File until the End of File is Reached. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Read Lines from a File until the End of File is Reached
 */
using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\sri\srip.txt";
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            using (StreamWriter sw = new StreamWriter(path))
            {
                sw.WriteLine("This");
                sw.WriteLine("text is");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }
 
            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        Console.Read();
    }
}
Program Explanation

This C# program is used to read lines from a file until the end of file is reached. We have defined the path of the file using ‘path’ variable. Using if condition statement check the path exists or not.

advertisement
advertisement

If the path exists then execute if condition statement and delete the path of the file. The StreamWriter writes text files and this program read lines from a file that is created. Print the read and end lines of a file.

Runtime Test Cases
 
This 
text is
to test
reading

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.