Skip to content

Instantly share code, notes, and snippets.

@nramsbottom
Created May 13, 2014 17:54
Show Gist options
  • Select an option

  • Save nramsbottom/be1e80c37fc7c8658a8b to your computer and use it in GitHub Desktop.

Select an option

Save nramsbottom/be1e80c37fc7c8658a8b to your computer and use it in GitHub Desktop.
StreamReader.ReadToEnd with maximum length
public static string ReadToEnd(this StreamReader reader, int maximumCharacters)
{
var builder = new System.Text.StringBuilder();
while (true)
{
var line = reader.ReadLine();
if (line == null)
break;
if (builder.Length + line.Length > maximumCharacters)
{
line = line.Substring(0, maximumCharacters - builder.Length - Environment.NewLine.Length);
builder.AppendLine(line);
break;
}
builder.AppendLine(line);
}
return builder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment