Reading and writing to csv file in c#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we will see how to read and write data to CSV file if you don't know me my name is ravindra sometimes I also write article in medium.com you can check out the link in the description before moving further make sure to subscribe this channel if you find this video helpful make sure to like the video and leave a comment in every video I provide the source code and you can find the link in the description this video might be little bit fast so pause the video whenever it is necessary so now let's get started first and foremost we are going to create console application here so search console here and select this option C sharp console app click on next now we need to put a project name here I'm going to write CSP reader demo here it is the name of the project and the solution select the framework mine is dotnet 7 which is the latest one right now click on the next now our project has been created and it is our default program.cs file I'm going to remove everything from here now let's create a name space here let's type namespace CSP reader demo let's create a class program inside side this name is space let's create a main method here and let's create another class person here we will have some properties here like name age and Country now we need to install a library called CSV helper in order to do this we need to go here in the tools nuget package manager manage nuget package for solution go to the browse Tab and search csb helper select this one CSV helper by Jos close select the project and click on the install click on I accept now this package has been installed now inside the class program let's create a static method static white static wire to write to CSV and it take a parameter string file path let's put a try cache block here first we will check if this file exists or not if it does not exist we need to create that file now let's see what is this keyword using using keyword tells us that we do not need to worry about disposal of object so now let's define some sample data which we are going to insert in the CSV file here we are going to write some configuration for CSV Helper and to this object we are passing a value culture info dot invariant it means we are not culture dependent right now has reader record tells whether we need a header or not in this file so let's set it true for now let's create an instance of stream writer here this using keyword tells us not to worry about disposal of this object stream let's create an instance of CSV writer it will take two parameters here one is stream writer and another one is config option let's write this people data to file make sure to use these namespaces in the top let's put an indicator it will tell us data has saved let's go to the main method copy the path where you want to create a file this directory path variable will contain the path don't forget to put at the rate sign in the beginning file name variable contains the name of file file path will contain the path of file and path dot combine method will help to create a path for us let's call write to CSV method and pass file path here to hold the screen we need to use console.read key method let's run the program you can see the data here and we have a header Also let's run this program once again if we open the file you will notice that it has overwritten the file to append the data to the file we need to pass second argument of stream writer as true let's run the program again and check the file now it has appended data but it has also appended header for now let's remove all the data to disable header in a file we need to set has header record to false let's run the program and check the file again now there is no header let's create some new dummy data and run the program now you can see that this data has appended successfully without any header for now let's comment this line now we will see how we can read data from the CSV file so let's create a static method read from CSV it will take one parameter string file path let's put a try cache block here first we will check if file exists or not if file exists we will move further here we have this config info same as the previous method it says that we are not looking for the header in the CSV file let's create an instance of stream reader it will take file path as an argument now create an instance of CSV reader it will take two parameters stream reader and configuration we would get all the data by using this CSV reader.getrecourse method and put it in variable records now we will Loop over the records and print it in the console let's call this method in the main method let's run the program and here is the output so now you can see that how easy is to read and write to CSV file and all thanks to CSV helper Library that's it for now I will see you next time
Info
Channel: Ravindra Devrani
Views: 3,381
Rating: undefined out of 5
Keywords:
Id: LxZGNwHq9kY
Channel Id: undefined
Length: 4min 51sec (291 seconds)
Published: Fri May 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.