Reading a File Line by Line C Program
In few previous articles, I have explained how we can read JSON data in C# and how to read excel file in C#, at present in this article, I have provided code sample using panel application to show how to read a text file in C# line by line or reading entire text file as a cord in i by go using C#.
Let'southward a look at each of the case code, one in which text file is read and converted into cord, i.e, using System.IO.ReadAllText()
and another is reading text file line by line using System.IO.ReadAllLines()
which returns array of line, and we can loop that array to print each line of text file.
Read File in .Net Framework 4.five Console awarding
Reading file in C# line by line
In this example, we will read a text file line past line using System.IO.ReadALLLines() in panel application.
Then, if you lot are new to C# or Visual Studio, you tin can create a new Console application by opening Visual Studio, navigating to "New"-> "Project" -> Select "Windows Classic" from left-pane and "Panel app (Windows Awarding)"-> Give a proper noun to your project "ReadInCSharp" and click "OK"
Now, inside Plan.cs, nosotros will write our code
using Organisation; using System.IO; namespace ReadInCSharp { course Program { static void Master(string[] args) { //file in disk var FileUrl = @"D:\testFile.txt"; //file lines cord[] lines = File.ReadAllLines(FileUrl); //loop through each file line foreach (cord line in lines) { Console.WriteLine(line); } } } }
Output:
This is exam file. To Read text file in C# Sample.
In the higher up, code nosotros are using foreach loop to read all lines of an cord array.
Reading text in C# all line at once
Let's take a wait at C# code to read all lines at once of a text file.
using Organization; using Organisation.IO; namespace ReadInCSharp { course Program { static void Main(string[] args) { //file in deejay var FileUrl = @"D:\testFile.txt"; // Read entire text file content in one string string text = File.ReadAllText(FileUrl); Console.WriteLine(text); } } }
Output:
This is test file. To Read text file in C# Sample.
Reading Text file using StreamReader
There is one more manner to read lines of a text file in C#, which is using StreamReader
.
StreamReader
class implements a TextReader that reads characters from a byte stream in a particular encoding.
using Organization; using Organisation.IO; namespace ReadInCSharp { class Program { static void Primary(cord[] args) { //file in disk var FileUrl = @"D:\testFile.txt"; try { // Create an instance of StreamReader to read from a file. // The using statement likewise closes the StreamReader. using (StreamReader sr = new StreamReader(FileUrl)) { string line; //read the line by line and print each line while ((line = sr.ReadLine()) != zippo) { Panel.WriteLine(line); } } } grab (Exception e) { // Something went incorrect. Console.WriteLine("The file could not be read:"); //impress error message Console.WriteLine(e.Message); } } } }
Output is same every bit above, in the abovde code, we are using StreamReader instance to read text from file.
As you tin meet in the in a higher place lawmaking, we are feeding the File url to "StreamReader
" grade object then we are reading file line by line using sr.ReadLine(), which gives the states one line at a time from text file, then using Panel.WriteLine()
, we are press the value of that line console application.
Read File in .NET Core Panel awarding
In the above case, we were reading file using .Internet framework, but you can also read files using 'StreamReader' in .NET Core, here is the working example.
Before, I show you case, I have created a new panel application using .NET Core in Visual Studio 2019 (Open Visual Studio -> Click on Create new project -> Select "Console App (.NET Core)" from templates -> Click "Next", give your project a name "ReadFileInNetCore" -> Click "Create")
Considering yous have text file at location "D:\testFile.txt", you can use the beneath C# Code in .NET Cadre to read text file line by line.
using System; using System.IO; namespace ReadFileInNetCore { class Plan { static void Main(string[] args) { FileStream fileStream = new FileStream(@"D:\testFile.txt", FileMode.Open); //read file line by line using StreamReader using (StreamReader reader = new StreamReader(fileStream)) { string line = ""; while ((line = reader.ReadLine()) != null) { //print line Console.WriteLine(line); } } Console.WriteLine("Printing any fundamental to continue"); Console.ReadKey(); } } }
If y'all will encounter the above lawmaking, you will notice, there isn't any difference in C# Code, when working with .Net four.v or .NET Cadre.
Output:
To read all files at once, y'all can use "ReadAllText" as mentioned for .Cyberspace Framework "System.IO.File.ReadAllText("YourFileLocatio.txt");
"
Notation: If you are working with .NET Core three and working with web-application, and you want to read file from wwwroot location, y'all can locate "wwwroot" folder every bit beneath:
private readonly IWebHostEnvironment _webHostEnvironment; public YourController (IWebHostEnvironment webHostEnvironment) { _webHostEnvironment= webHostEnvironment; } public IActionResult Index() { cord webRootPath = _webHostEnvironment.WebRootPath; string contentRootPath = _webHostEnvironment.ContentRootPath; string path =""; path = Path.Combine(webRootPath , "yourFolder"); //or path = Path.Combine(contentRootPath , "wwwroot" ,"yourFolder" ); return View(); }
Y'all may also similar to read:
Read PDF file in C# using iTextSharp
Source: https://qawithexperts.com/article/c-sharp/read-file-in-c-text-file-example-using-console-application/262
0 Response to "Reading a File Line by Line C Program"
Post a Comment