How to Read a File in C#

Tue, Nov 8, 2016

Read in 1 minutes

Csharp was probably the first compiled language I really loved. When the first version of .net came out I wrote a small CMS using C# and the new runtime. (A big step up from old school asp)

Reading a file - here as a stream - is easy. Setup a streamreader and call ReadLine on it, until it returns null.

string line;
int linenum;
System.IO.StreamReader file = new System.IO.StreamReader("test.csharp");
while((line = file.ReadLine()) != null) {
  linenum++;
  Console.WriteLine (linenum + " : " + line);
}

We prefix each line with a linenumber.

See Also

How to Read a File in Lua

Fri, Oct 28, 2016

Read more →

How to Read a File in C++

Tue, Oct 4, 2016

Read more →

How to read a file in Java

Tue, Sep 13, 2016

Read more →

How to Read a file in JavaScript

Fri, Nov 27, 2015

Read more →

How to Read a file in Go

Thu, Nov 26, 2015

Read more →