Read and write a txt file in C#

In the case you want to read or write some short information from or into a text file, Csharp supports a simple way for you in System.IO.File. You create a text file in directory “C:\\config.txt” and then:
Read file
There are some lines in above file, and each line has its own meaning, so you need to read every line and write into a list or an array like this:

private void readFile()
    string[] lines = System.IO.File.ReadAllLines(@"C:\config.txt");
    //then you can convert every element of the array into the data you want

Write file
You have some information need to be stored in a text file, you can use the below function:

private void writeFile()
    string[] lines = { lstCube.Count.ToString(), numOfXRect.ToString(), count.ToString() };

Are they so simple,hihi, i just want to store in case i need.:D

One thought on “Read and write a txt file in C#

  1. I’m impressed, I must say. Rarely do I come across a blog that’s both educative and entertaining, and let me tell you,
    you have hit the nail on the head. The problem is something which not enough men and women are speaking intelligently
    about. I’m very happy that I came across this during my hunt
    for something regarding this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.