Loading data from a txt file - C#

…Continuation of this

Now I’ve created a text file with code… Now I just need to read the text i’ve written down in the .txt
-how?

-thanks :slight_smile:

EDIT: Fixed the link

link to my next question

Your link doesn’t seem to work.

Generally, you use a streamreader to load in text. Here’s some code (untested) that should get you on the right track:

using System.Text;
using System.IO;  

private bool Load(string fileName)
{
    // Handle any problems that might arise when reading the text
    try
    {
        string line;
        // Create a new StreamReader, tell it which file to read and what encoding the file
        // was saved as
        StreamReader theReader = new StreamReader(fileName, Encoding.Default);

        // Immediately clean up the reader after this block of code is done.
        // You generally use the "using" statement for potentially memory-intensive objects
        // instead of relying on garbage collection.
        // (Do not confuse this with the using directive for namespace at the 
        // beginning of a class!)
        using (theReader)
        {
            // While there's lines left in the text file, do this:
            do
            {
                line = theReader.ReadLine();
                    
                if (line != null)
                {
                    // Do whatever you need to do with the text line, it's a string now
                    // In this example, I split it into arguments based on comma
                    // deliniators, then send that array to DoStuff()
                    string[] entries = line.Split(',');
                    if (entries.Length > 0)
                        DoStuff(entries);
                }
            }
            while (line != null);

            // Done reading, close the reader and return true to broadcast success    
            theReader.Close();
            return true;
            }
        }

        // If anything broke in the try block, we throw an exception with information
        // on what didn't work
        catch (Exception e)
        {
            Console.WriteLine("{0}

", e.Message);
return false;
}
}
}

Similar to the answer to the original question referenced, you could use the File.ReadAllText method:

string text = System.IO.File.ReadAllText("myfile.txt");

It’s better do

     using (theReader)
     {
         line = theReader.ReadLine();

         if(line != null){
                 // While there's lines left in the text file, do this:
                 do
                 {
                        // Do whatever you need to do with the text line, it's a string now
                        // In this example, I split it into arguments based on comma
                        // deliniators, then send that array to DoStuff()
                        string[] entries = line.Split(',');
                        if (entries.Length > 0)
                        DoStuff(entries);

                        line = theReader.ReadLine();
                 }
                 while (line != null);
         } 
         // Done reading, close the reader and return true to broadcast success    
         theReader.Close();
         return true;
         }
   }

It uses an if less inside the do-while