如何在C#中从控制台读取很长的输入?

Dra*_*his 5 c# console windows-console

我需要在C#中从控制台加载veeeery long line,最多65000个字符.Console.ReadLine本身的限制为254个字符(转义序列为+2),但我可以使用:

static string ReadLine()
{
    Stream inputStream = Console.OpenStandardInput(READLINE_BUFFER_SIZE);
    byte[] bytes = new byte[READLINE_BUFFER_SIZE];
    int outputLength = inputStream.Read(bytes, 0, READLINE_BUFFER_SIZE);
    Console.WriteLine(outputLength);
    char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
    return new string(chars);
}
Run Code Online (Sandbox Code Playgroud)

...克服这个限制,最多8190个字符(转义序列+2) - 不幸的是我需要输入WAY更大的行,当READLINE_BUFFER_SIZE设置为大于8192的任何值时,错误"没有足够的存储空间可供处理这个命令"显示在VS. 缓冲区应该设置为65536.我已经尝试了几个解决方案来做到这一点,但我还在学习并且没有超过1022或8190个字符,我怎么能将该限制增加到65536?提前致谢.

hag*_*ago 2

尝试使用 StringBuilder Console.Read

        StringBuilder sb =new StringBuilder();
        while (true) {
            char ch = Convert.ToChar(Console.Read());
            sb.Append(ch);
            if (ch=='\n') {
                break;
            }
        }
Run Code Online (Sandbox Code Playgroud)