How to Read a Text File in C# Windows Forms Application
In few previous manufactures, I take explained how we can read JSON data in C# and how to read excel file in C#, now in this article, I take provided code sample using console awarding to show how to read a text file in C# line by line or reading entire text file as a string in one by get using C#.
Let'southward a look at each of the example lawmaking, one in which text file is read and converted into string, i.e, using System.IO.ReadAllText()
and another is reading text file line past 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 four.five Console application
Reading file in C# line by line
In this example, nosotros will read a text file line past line using System.IO.ReadALLLines() in console application.
So, if you are new to C# or Visual Studio, you can create a new Console application past opening Visual Studio, navigating to "New"-> "Project" -> Select "Windows Archetype" from left-pane and "Console app (Windows Awarding)"-> Give a name to your projection "ReadInCSharp" and click "OK"
Now, inside Plan.cs, we volition write our code
using System; using Organisation.IO; namespace ReadInCSharp { class Program { static void Master(string[] args) { //file in disk var FileUrl = @"D:\testFile.txt"; //file lines string[] 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 above, code we are using foreach loop to read all lines of an string assortment.
Reading text in C# all line at once
Allow'due south accept a look at C# code to read all lines at one time of a text file.
using System; using Arrangement.IO; namespace ReadInCSharp { class Programme { static void Primary(string[] args) { //file in deejay var FileUrl = @"D:\testFile.txt"; // Read entire text file content in one string string text = File.ReadAllText(FileUrl); Panel.WriteLine(text); } } }
Output:
This is exam file. To Read text file in C# Sample.
Reading Text file using StreamReader
At that place is i more way to read lines of a text file in C#, which is using StreamReader
.
StreamReader
form 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 Master(string[] args) { //file in disk var FileUrl = @"D:\testFile.txt"; effort { // Create an instance of StreamReader to read from a file. // The using statement also closes the StreamReader. using (StreamReader sr = new StreamReader(FileUrl)) { string line; //read the line by line and print each line while ((line = sr.ReadLine()) != cypher) { Console.WriteLine(line); } } } catch (Exception e) { // Something went wrong. Console.WriteLine("The file could not be read:"); //print error bulletin Panel.WriteLine(e.Message); } } } }
Output is same as in a higher place, in the abovde code, we are using StreamReader example to read text from file.
As y'all can see in the above lawmaking, nosotros are feeding the File url to "StreamReader
" class object so nosotros are reading file line by line using sr.ReadLine(), which gives united states ane line at a time from text file, then using Console.WriteLine()
, we are press the value of that line console application.
Read File in .Internet Core Console application
In the above example, we were reading file using .NET framework, only you can too read files using 'StreamReader' in .NET Core, here is the working example.
Before, I testify you instance, I accept created a new panel application using .Net Core in Visual Studio 2022 (Open up Visual Studio -> Click on Create new project -> Select "Panel App (.NET Core)" from templates -> Click "Side by side", give your project a proper name "ReadFileInNetCore" -> Click "Create")
Considering you have text file at location "D:\testFile.txt", y'all can utilise the beneath C# Code in .NET Core to read text file line by line.
using Arrangement; using System.IO; namespace ReadFileInNetCore { grade Plan { static void Primary(string[] args) { FileStream fileStream = new FileStream(@"D:\testFile.txt", FileMode.Open up); //read file line by line using StreamReader using (StreamReader reader = new StreamReader(fileStream)) { string line = ""; while ((line = reader.ReadLine()) != zip) { //print line Panel.WriteLine(line); } } Panel.WriteLine("Press any key to continue"); Console.ReadKey(); } } }
If yous will see the to a higher place lawmaking, y'all will observe, there isn't whatsoever difference in C# Lawmaking, when working with .NET 4.5 or .NET Core.
Output:
To read all files at once, you can employ "ReadAllText" as mentioned for .Net Framework "System.IO.File.ReadAllText("YourFileLocatio.txt");
"
Note: If you are working with .NET Core 3 and working with web-application, and yous desire to read file from wwwroot location, you tin can locate "wwwroot" folder as beneath:
private readonly IWebHostEnvironment _webHostEnvironment; public YourController (IWebHostEnvironment webHostEnvironment) { _webHostEnvironment= webHostEnvironment; } public IActionResult Index() { string webRootPath = _webHostEnvironment.WebRootPath; string contentRootPath = _webHostEnvironment.ContentRootPath; string path =""; path = Path.Combine(webRootPath , "yourFolder"); //or path = Path.Combine(contentRootPath , "wwwroot" ,"yourFolder" ); return View(); }
You may also like 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 "How to Read a Text File in C# Windows Forms Application"
Post a Comment