Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to programmatically clear the console history?

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.

like image 685
adrianbanks Avatar asked Jul 14 '10 09:07

adrianbanks


People also ask

How do you clear your console history?

Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console.

How do you clear the console with code?

Use Ctrl + K . This goes clean your console in Visual Studio Code.

What does console Clear () do?

clear() The console. clear() method clears the console if the console allows it. A graphical console, like those running on browsers, will allow it; a console displaying on the terminal, like the one running on Node, will not support it, and will have no effect (and no error).


2 Answers

Could this post on How can I configure the command line history, DOSKEY? help?

In olden DOS days a utility was available, DOSKEY.EXE, which enabled the user to cycle through previous commands. In NT this is enabled by default and you can cycle through old commands however DOSKEY has other abilities.

To clear the current command line history use command:

doskey /reinstall

You can also optionally tell it how many old commands to keep with the /listsize parameter

doskey /reinstall /listsize=50

would keep 50 old commands.

Please let me know if it works and how you used it :)

like image 195
Svish Avatar answered Oct 01 '22 08:10

Svish


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

like image 41
Hans Olsson Avatar answered Oct 01 '22 07:10

Hans Olsson