0

我有一行大约 1.5kb 的文本。我希望我的控制台应用程序可以读取它,但只能粘贴前 255 个字符。如何增加此限制?我实际上是在 Visual Studio 2013 下的调试模式下使用 Console.ReadLine() 阅读它

4

2 回答 2

2

MSDN类似这样的东西应该可以工作:

Stream inputStream = Console.OpenStandardInput();
byte[] bytes = new byte[1536];    // 1.5kb
int outputLength = inputStream.Read(bytes, 0, 1536);

您可以将字节数组转换为字符串,例如:

var myStr = System.Text.Encoding.UTF8.GetString(bytes);
于 2015-01-30T21:46:32.133 回答
2

这已经讨论过几次了。让我向您介绍迄今为止我看到的最佳解决方案(Console.ReadLine() 最大长度?

概念:使用 OpenStandartInput 验证 readline 函数(就像评论中提到的人一样):

实施

private static string ReadLine()
{
    Stream inputStream = Console.OpenStandardInput(READLINE_BUFFER_SIZE); // declaring a new stream to read data, max readline size
    byte[] bytes = new byte[READLINE_BUFFER_SIZE]; // defining array with the max size
    int outputLength = inputStream.Read(bytes, 0, READLINE_BUFFER_SIZE); //reading
    //Console.WriteLine(outputLength); - just for checking the function
    char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength); // casting it to a string
    return new string(chars); // returning
}

通过这种方式,您可以从控制台中获得最大的收益,并且它可以工作超过 1.5 KB。

于 2015-01-30T21:47:32.237 回答