14

Possible Duplicate:
Console.Readline() max length?

In my attempt to find a very simple text to speech application I decided it was faster to write my own. I noticed, however, that Console.Readline() is limited in the amount of text it allows per line to 254 characters; I can't find anything about this limit in the method documentation.

Is this a limitation in the Windows stack, or a problem with my code? How can I overcome it? I could decide to read character by character with Console.Readkey(), but won't I then risk losing characters to the MS DOS dumb text pasting behavior?

Community
  • 1
  • 1
badp
  • 11,409
  • 3
  • 61
  • 89
  • Please note I understand where 254 must come from - that's 256 bytes, minus a new line and the feed return. I don't know where that 256 came from, however. – badp May 21 '11 at 13:11

2 Answers2

48

This is a somewhat bizarre limitation on the Console API. I had this problem before and found the following solutions:

Console.SetIn(new StreamReader(Console.OpenStandardInput(8192)));

From the following MSDN forum post:

http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/51ad87c5-92a3-4bb3-8385-bf66a48d6953

See also this related StackOverflow question:

Console.ReadLine() max length?

Dale K
  • 25,246
  • 15
  • 42
  • 71
ColinE
  • 68,894
  • 15
  • 164
  • 232
  • 2
    for me I needed the following: Console.SetIn(new StreamReader(Console.OpenStandardInput(8192), Console.InputEncoding, false, 8192)); – Perrin255 Sep 01 '15 at 12:54
7

A quick look at implementation with .NET Reflector gives this:

public static Stream OpenStandardInput()
{
    return OpenStandardInput(0x100);
}

public static Stream OpenStandardInput(int bufferSize)
{
  ...
}

256 is the default value of OpenStandardInput, so I guess it's by design. Note this is only for .NET as the Windows API does not have this limit.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298