- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to read all the lines one by one in a file
Use ReadAllLines() method to read all the lines one by one in a file.
Let’s say we have a file “new.txt” with the following lines.
One Two Three
Firstly, set the path of the file to be read.
String myPath = "new.txt";
Now add it under a string array to fetch the lines on by one.
String[] fLine = File.ReadAllLines(myPath);
Let’s say you need to fetch the first line. For that.
fLine[0]
The following is the complete example that reads lines one by one in a file.
Example
using System; using System.IO; public class Demo { public static void Main() { String myPath = "new.txt"; String[] fLine; // array of lines in a file fLine = File.ReadAllLines(myPath); // read lines of a file Console.WriteLine("Line 1: "+fLine[0]); Console.WriteLine("Line 2: "+fLine[1]); Console.WriteLine("Line 3: "+fLine[2]); Console.WriteLine("Line 4 "+fLine[3]); } }
Output
Line1: One Line2: Two Line3: Three Line4: Four
- Related Articles
- C# Program to read all the lines of a file at once
- Python Program to Read First n Lines of a File
- C++ program to read file word by word?
- C program to copy the contents of one file to another file?
- How to read data from one file and print to another file in Java?
- C# Program to count the number of lines in a file
- C Program to count the number of lines in a file?
- How to programmatically add buttons into a layout one by one in several lines in Android
- C Program for copying the contents of one file into another file
- C++ program to append content of one text file to another
- C program to replace all zeros with one in a given integer.
- Golang Program to Read the Contents of a File
- Read file line by line using C++
- Saving all the open Matplotlib figures in one file at once
- Write a C program to read a data from file and display

Advertisements