Sign in to follow this  

How to read file line

This topic is 1820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How can I read file line. For example I create file settings.dat and i want to store two lines in it, color id, and username. How do i need to do. I tried to google but it was not worth it. :(

Share this post


Link to post
Share on other sites
Judging by the question, you do not only want to read a line from a file, but also write it, right?

Writing:
TextWriter writer = new StreamWriter("settings.dat");
writer.WriteLine(colorId);
writer.WriteLine(userName);
writer.Close();
Reading:
TextReader reader = new StreamReader("settings.dat");
String colorId = reader.ReadLine();
String userName = reader.ReadLine();
reader.Close();
Note that you also should do some error checking, right now bad things will happen if, for example the file cannot be opened or the file does not have two lines in it... Edited by brx

Share this post


Link to post
Share on other sites
while (File.Exists("Settings.dat"))
        {
            options.username = reader.ReadLine();

            Console.WriteLine("User: " + options.username);
            reader.Close();
        }

What is wrong in here?

Edited by TheScriptan

Share this post


Link to post
Share on other sites
Sign in to follow this