When working with a console application, a history of everything that has been entered at a Console.ReadLine()
is stored. When at a console prompt to enter something, pressing the up/down cursor will scroll through this history (and the whole history can be viewed by pressing F7).
Using C#, is there are way to either disable this behaviour or clear the history of what has already been entered?
To clarify, Console.Clear()
does not clear the history, only the screen buffer. I want to clear the command history.
EDIT: Having tried several of the suggested methods, as well as some of my own devising, the best approach is the one suggested by ho1. It is not ideal because it brings up another console window, but it does clear the history.
Could this post on How can I configure the command line history, DOSKEY? help?
Please let me know if it works and how you used it :)
You could try using the property
Edit: Removed incorrect answer (I got confused about what you wanted to do) and added another (hopefully) better answer.
It might be possible to do this by freeing the current console using FreeConsole and then allocating a new console using AllocConsole. I'd assume that it wouldn't keep the command line history then.
In general, if you want to do things with the Console that's not supported by the .Net Framework, this MSDN page is a good place to look: Console Functions
It seems according to MSN (http://msdn.microsoft.com/en-ie/library/system.console_members.aspx) the method
console.clear()
"Clears the console buffer .."This code is working for me to clear the command history. Limited testing and no guarantees on portability.
Take a look at SetConsoleHistoryInfo. According to the documentation it only works on Vista and above, so I'm not sure if it will help you or not.