Monthly Archives: August 2013

Reading a file with C#

      No Comments on Reading a file with C#

Reading a text file line by line in C#. Dont forget to include using System.IO; StreamReader myReader = new StreamReader(@”c:\test.txt”); string line = “”; while (line != null) { line = myReader.ReadLine(); Console.WriteLine(line); } You can read all the text within a file using the ReadAllText function. string filename =… Read more »

Explicit Conversion of a string to Integer using c#

Explicit Conversion of a string to Integer, sometimes known as cast. string myString = “7”; int myInt = int.Parse(myString); Console.WriteLine(myInt); The above code will fail if the string can not be converted to a integer. For example if myString=”Seven”, the code would fail. To avoid this problem use TryParse. string… Read more »