Why does Console.Readline() have a limit on the le

2019-03-25 20:43发布

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?

2条回答
狗以群分
2楼-- · 2019-03-25 21:21

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.

查看更多
相关推荐>>
3楼-- · 2019-03-25 21:37

This is a somewhat bizarre limitation on the Console API. I had this problem before and found the followig 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?

查看更多
登录 后发表回答