views:

142

answers:

5

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.

+4  A: 

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 :)

Svish
Works on xp SP3.
mavnn
I want to do this programmatically from within my own console application, so I do not want to have to rely on using an external tool.
adrianbanks
What if the functionality you are looking to suppress is provided by that same tool?
Paul-Jan
@Svish: I've tried to call out to this from within my code, but it has no effect because it is launched in a separate console. It appears that I need to do this within code to get it to apply to the **current** console.
adrianbanks
@adrianbanks - Right... Yeah, I thought you might. How did you call out to it? Is there a way to find out what the command does in the background? Like I think @Paul-Jan is referring to
Svish
@Svish: Ok, I'm a bit confused now! I called out to doskey by starting a new process. How else can I call out to it from C#?
adrianbanks
@adrianbanks - Have a look at the `Start` method that takes a `ProcessStartInfo` class. For example there is two properties called `CreateNoWindow` and `UseShellExecute`. Maybe they can help with something? - http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx
Svish
@Svish: Yeah, already tried that with no joy. I cannot see how launching doskey will work as it will always be a new process.
adrianbanks
@adrianbanks - Well, I was thinking that it is a new process when you run it from a standard console too. But anyways, seems like you hvave found a working solution =)
Svish
A: 

It seems according to MSN (http://msdn.microsoft.com/en-ie/library/system.console_members.aspx) the method console.clear() "Clears the console buffer .."

bAN
He wants to clear the command history, not the console buffer (if I understood correctly)
Svish
ok.. I thought that buffer and history was bound.
bAN
A: 

You could try using the property

Console.BufferHeight : "This property defines the number of rows (or lines) stored in the buffer that is accessed by a console mode window"
mishal153
I already tried that, but it will not let me set a value of zero.
adrianbanks
BufferHeight is the scrollable window of all console output. That's why there's also BufferWidth.
Agent_9191
+1  A: 

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

ho1
Doesn't do anything to the history I'm afraid.
adrianbanks
@adrianbanks: Yep, got confused about what you were doing (though at least I made a different mistake than the other answers to this question :)). I've updated my answer with another possible solution.
ho1
+1  A: 

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.

aphoria