How to Read From a File a Count Selected Words in C#
In few previous articles, I have explained how we tin can read JSON data in C# and how to read excel file in C#, now in this commodity, I have provided lawmaking sample using console awarding to evidence how to read a text file in C# line past line or reading entire text file as a string in 1 by get using C#.
Let'south a look at each of the example code, ane 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 4.5 Console application
Reading file in C# line by line
In this example, we will read a text file line by line using System.IO.ReadALLLines() in panel awarding.
So, if you are new to C# or Visual Studio, you can create a new Console awarding by opening Visual Studio, navigating to "New"-> "Project" -> Select "Windows Classic" from left-pane and "Console app (Windows Application)"-> Give a proper noun to your project "ReadInCSharp" and click "OK"
Now, inside Plan.cs, we volition write our code
using Arrangement; using System.IO; namespace ReadInCSharp { class Program { static void Main(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 test 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 one time
Let'southward accept a await at C# code to read all lines at once of a text file.
using Organization; using System.IO; namespace ReadInCSharp { course Program { static void Main(string[] args) { //file in disk 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 1 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 System; using System.IO; namespace ReadInCSharp { class Program { static void Principal(cord[] args) { //file in disk var FileUrl = @"D:\testFile.txt"; endeavor { // Create an example of StreamReader to read from a file. // The using statement besides closes the StreamReader. using (StreamReader sr = new StreamReader(FileUrl)) { string line; //read the line by line and print each line while ((line = sr.ReadLine()) != cipher) { Panel.WriteLine(line); } } } catch (Exception due east) { // Something went wrong. Console.WriteLine("The file could not be read:"); //print error message Console.WriteLine(e.Message); } } } } Output is same as in a higher place, in the abovde lawmaking, nosotros are using StreamReader case to read text from file.
Equally you can run across in the above code, we are feeding the File url to "StreamReader" grade object and and so we are reading file line by line using sr.ReadLine(), which gives united states i line at a time from text file, then using Console.WriteLine(), nosotros are printing the value of that line console awarding.
Read File in .Internet Cadre Console application
In the to a higher place instance, nosotros were reading file using .Cyberspace framework, merely y'all can also read files using 'StreamReader' in .Net Core, here is the working example.
Earlier, I show you lot example, I have created a new console application using .Cyberspace Cadre in Visual Studio 2019 (Open Visual Studio -> Click on Create new project -> Select "Console App (.Internet Core)" from templates -> Click "Adjacent", give your projection a name "ReadFileInNetCore" -> Click "Create")
Considering you lot have text file at location "D:\testFile.txt", you lot tin use the below C# Code in .NET Core to read text file line past line.
using System; using Organisation.IO; namespace ReadFileInNetCore { form Plan { static void Master(string[] args) { FileStream fileStream = new FileStream(@"D:\testFile.txt", FileMode.Open); //read file line past line using StreamReader using (StreamReader reader = new StreamReader(fileStream)) { string line = ""; while ((line = reader.ReadLine()) != naught) { //print line Panel.WriteLine(line); } } Console.WriteLine("Press any key to continue"); Console.ReadKey(); } } } If you will meet the above code, y'all volition notice, at that place isn't any difference in C# Code, when working with .Net 4.v or .NET Core.
Output:
To read all files at once, y'all can use "ReadAllText" as mentioned for .Cyberspace Framework "Arrangement.IO.File.ReadAllText("YourFileLocatio.txt");"
Annotation: If yous are working with .NET Cadre 3 and working with web-application, and you want to read file from wwwroot location, you can locate "wwwroot" folder equally beneath:
private readonly IWebHostEnvironment _webHostEnvironment; public YourController (IWebHostEnvironment webHostEnvironment) { _webHostEnvironment= webHostEnvironment; } public IActionResult Index() { cord webRootPath = _webHostEnvironment.WebRootPath; cord contentRootPath = _webHostEnvironment.ContentRootPath; cord path =""; path = Path.Combine(webRootPath , "yourFolder"); //or path = Path.Combine(contentRootPath , "wwwroot" ,"yourFolder" ); return View(); } You may as well like to read:
Read PDF file in C# using iTextSharp
How to Read From a File a Count Selected Words in C#
Source: https://qawithexperts.com/article/c-sharp/read-file-in-c-text-file-example-using-console-application/262
0 Response to "How to Read From a File a Count Selected Words in C#"
Post a Comment