Archive

Archive for November, 2010

Reading Text Files in .NET – The Easy Way

November 21st, 2010 No comments

I don't know about you fellow .NET programmers, but I don't often come across a need to read from text files often when I am programing in .NET. As such, I find that every time I need to do this, I have to use Google to refresh myself on how to do it. Recently, i found a much easier  way of reading text files that I felt compelled to share.

VB.NET
Dim s as String
Dim file_path as String = "C:\test.txt"

'Storing the text from the test.txt file in a string variable
s = System.IO.File.ReadAllText(file_path)
C#
string s;
string file_path = "C:\test.txt";

//Storing the text from the test.txt file in a string variable
s = System.IO.File.ReadAllText(file_path);

I hope this is as useful to you as it was for me.

Categories: C#, Code Snippets, VB.NET Tags: ,