Why does Console.Readline() have a limit on the length of text it allows? [duplicate]

血红的双手。 提交于 2020-01-01 04:31:09

问题


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?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/6081946/why-does-console-readline-have-a-limit-on-the-length-of-text-it-allows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!