c# read file streamreader code example
Example 1: c# streamreader to file
// after .NET 4.0
using (var fileStream = File.Create("C:\\Path\\To\\File"))
{
myOtherObject.InputStream.Seek(0, SeekOrigin.Begin);
myOtherObject.InputStream.CopyTo(fileStream);
}
// before .NET 4.0
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ( (len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
// streams are not closed
}
Example 2: c# read file stream
//this will get a string with all the text from the file
var fileText = File.ReadAllText(@"path\to\my\file.txt");
//this will get all of the lines of the file as an string[]
var fileLines = File.ReadAllLines(@"path\to\my\file.txt");
Example 3: C# using StreamReader
using System.IO; // <- This is the class for StreamReader