C# Data Access: Text Files


To sign up for announcements about new videos and exclusive discounts, fill out this form: (your email will be kept safe and you will not be spammed).

Data access in C# is not really hard, but there is a lot of conflicting information out there. This video will help you clear up how to read from and write to a text file. It doesn’t cover every method, just one of the easiest.


Xem Thêm Bài Viết Khác:https://wijstaanvooronzegrondrechten.org/cong-nghe


  1. Dear Tim Corey,

    Is there any way to insert a string into a specific row location of a .csv file?
    (e.g. I want to insert a string to the second row of a CSV file which has 10 rows data)

  2. its giving me that last line in the text box
    here is my code
    richTextBox1.Visible = true;

    string path = @"C:AL.txt";

    List <string> lines = File.ReadAllLines(path).ToList();

    foreach (string line in lines) {

    richTextBox1.Text = line;


  3. Hey Tim, what's the difference between using a streamwriter/reader or just simply using File.(…) ? Both can achieve the same thing. And thanks for the videos, great help!

  4. Adding to a string array is difficult so you used a List Collection. But why didn't you use StringBuilder type which easily supports adding data ? Kindly answer.

  5. Wow didnt know about interpolated strings ($"") being faster than " "+"…" Etc.This i will use in the future even for convenience and perfrmance gain.Thanks!

  6. hello, i'm a student studying C# because of our final project..can anyone help me please about merging three text files which are from saved points gathered from zedgraph to be separated by comma..

    for example

    file1.txt contains




    file2.txt contains




    file3.txt contains




    through which i need to have an output of

    1 , 4 , 7

    2 , 5 , 8

    3 , 6 , 9

    …thank you in advance.. this is very urgent and important..if you have any codes available..tnx.

  7. Very informative video helped me out a lot, but I have a question. Is there a way to pass text from the file into a 2D array. so each comma represents a column, and line represents a row to make up a table.

    mammals, Dogs, cats, horses, Platypus
    Fish, Clownfish, Salmon, Crayfish, Tuna
    Birds, Parrots, Pigeons, Sparrows, Penquin

    The point is to make an easy way to randomly choose a category and then display the elements within it.

  8. No matter where I place the file, and write the file path EXACTLY as i have it structured, the compiler will not find the file and runs an exception. I cant seem to find anything about how to fix this online.
    EDIT: Pretty sure that it was reading the file "Test.txt" as "Test.txt.txt". When i removed the ".txt" part of the file name, the program compiled normally. I found that solution on stackoverflow and it helped out. Anyways, it was a naming problem on my end. Sorry for the mix up and thanks for the tutorials 🙂

  9. Hey Guyss! when i run the program, it says System.IO.filenotfoundexception.
    the directory was fine and i checked it
    May i knoe the solution?

  10. Hi Tim, what if two fields in the text file are numbers and you wanted to calculate the sum of the two numbers in a new field then output that into the same text file?

  11. Need help, What's actually happening here: List<Person> People = new List<Person>(); ?
    Are you declaring a list and its type is Person because it's a list of objects from class Person?

  12. Hello, Tim! First of all, thank you for this video, it really helped me out a lot on a project i'm working on. I have a question though. I want to be able to search for the first element in the file and have the program return the rest of the line to me.

    Here is an example –
    Die: 747 measurement: 266.7 Notes: Some sample note here.

    Something like this but I want to search by die, in other words, the first element. Thanks in advance. Best of luck to you.

  13. Will this work for reading .cfg files if I alter the code to look for that file instead, also would this work for Unity, since it might be using the same concept, just replace most of the logging with Debug.Log?

  14. Can't belive it! 80% of my work is based on text files, move it, make logs about the actions taken, update, read it to valid them, awesome Tim, you get a great way to teach. I am your Fan!! Its pure gold. Thanks so much!


Please enter your comment!
Please enter your name here