CodeNoob3141
CodeNoob3141

Reputation: 1

In C# VS2013 how do you read a resource txt file one line at a time?

             static void Starter(ref int[,] grid)
                {
                    StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(Resources.Sudoku));
                    string line = reader.ReadLine();

                    Console.Write(line);
                    Console.ReadLine();           
                }

I know this isn't right, but it gets my point across. I would like to be able to read in the resource file one line at a time. Like so:

                    System.IO.StreamReader StringFromTxt 
                        = new System.IO.StreamReader(path);
             string line = StringFromTxt.ReadLine();

I do not necessarily have to read in from the resource, but I am not sure of any other way to call a text file without knowing the directory every time, or hard coding it. I can't have the user pick files.

Upvotes: 0

Views: 205

Answers (2)

Jimi Shah
Jimi Shah

Reputation: 15

StreamReader sr = new StreamReader("D:\\CountryCodew.txt"); while (!sr.EndOfStream) { string line = sr.ReadLine(); }

Upvotes: 2

Boris
Boris

Reputation: 586

MSDN lists the following as the way to read in one line at a time:
https://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx

int counter = 0;  //keep track of #lines read
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();

Additional examples for getline:
https://msdn.microsoft.com/en-us/library/2whx1zkx.aspx

Upvotes: 0

Related Questions