.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: