Increase buffer for Console.Readline?

三世轮回 提交于 2019-12-13 04:43:27

问题


I have a line that is about 1.5kb of text. I want my console app to read it but only the first 255 characters can be pasted in. How do I increase this limit? I am literally reading it using Console.ReadLine() in debug mode under visual studio 2013


回答1:


From MSDN something like this ought to work:

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

The you can convert your byte array to a string with something like:

var myStr = System.Text.Encoding.UTF8.GetString(bytes);



回答2:


This have been already discussed a couple times. Let me introduce you to the best solution i saw so far ( Console.ReadLine() max length? )

The concept: verriding the readline function with OpenStandartInput (like the guys in the comments mentioned):

The implementation:

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
}

This way you get the most you can from the console, and it'll work for more than 1.5 KB.



来源:https://stackoverflow.com/questions/28245018/increase-buffer-for-console-readline

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