C# - Read Text File - Whole Content - Line by Line
[Pages:4]C# ? Read Text File ? Whole Content ? Line by Line
C# Read Text File ? Whole Content
To read a text file using C# programming, follow these steps.
1. Import System.IO. We are going to use File class. 2. Use File.ReadAllText() method with path to the file and encoding passed as arguments. 3. ReadAllText() returns a string which is the whole text in the text file.
Program.cs
using System.IO; using System.Text; namespace CSharpExamples {
class Program { static void Main(string[] args) { string textFileContent = File.ReadAllText(@"D:\sample.txt",Encoding.UTF8); System.Console.WriteLine(textFileContent); }
} }
We have used Encoding.UTF8 of System.Text to specify the encoding of the file we are reading.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run This is C# Tutorial from . This is a sample text file.
C# Read Text File ? Line by Line
To read a text file line by line using C# programming, follow these steps.
1. Import System.IO for function to read file contents. 2. Import System.Text to access Encoding.UTF8. 3. Use FileStream to open the text file in Read mode. 4. Use StreamReader to read the file stream.
4. Use StreamReader to read the file stream. 5. Then we can use while loop to read each line of the text file with StreamReader.ReadLine() function.
Reading a file line by line is really useful if the size of text file is huge.
Program.cs
using System.IO; using System.Text; namespace CSharpExamples {
class Program { static void Main(string[] args) { var fileStream = new FileStream(@"d:\sample.txt", FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { string line; while ((line = streamReader.ReadLine()) != null) { System.Console.WriteLine("- "+line); } } }
} }
Output
PS D:\workspace\csharp\HelloWorld> dotnet run - This is C# Tutorial from . - This is a sample text file.
Conclusion
In this C# Tutorial, we learned to read a file's content as a whole and store it in a string or read the file line by line.
C# Tutorial C# Tutorial C# Basic Example C# Comments C# Variables C# Constants C# if, if-else C# switch
C# switch C# while loop C# for loop C# foreach C# break C# continue C# struct C# enum C# String C# Array C# Command Line Arguments
C# Console Operations C# Write to Console C# Read from Console
C# Object Oriented Programming Concepts C# Class & Constructors C# Encapsulation C# Polymorphism C# Method Overloading C# Interfaces
C# String Operations C# String Length C# Substring
C# File Operations C# Read Text File C# Write to File C# Delete File C# Copy File
C# Exception Handling C# try-catch
C# try-catch C# finally C# throw C# Custom Exception C# SystemException C# DivideByZeroException C# NullReferenceException C# InvalidCastException C# IOException C# FieldAccessException
C# Collections C# List C# SortedList C# HashSet C# SortedSet C# Stack C# Queue C# LinkedList C# Dicionary C# SortedDictionary
C# Errors [Solved] C# Error: Class does not contain a constructor that takes arguments
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- line by line song lyric generator
- pandas read text file to dataframe
- python read text file pandas
- read text file in java
- powershell read text file loop each line
- python pandas read text file into dataframe
- read text file into list
- python read text file into array
- powershell read text file array
- how to read text file in python
- read text file python data
- python read text file as string