Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my console application have command history?

I have written a console application, which is essentially a Console.ReadLine()-Loop. When the application is waiting for input, pressing the up arrow key iterates through all previous lines of input. My application does not contain any code for this feature. What part of Windows provides this? How can I disable it?

I can only image that it's either a feature of the console subsystem or implemented in Console.ReadLine().

Here is some sample code that exhibits the described behavior:

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            do
            {
                input = System.Console.ReadLine();
            } while (input != "exit");
        }
    }
}

I would like to disable the history feature for now, and re-implement it later using my own code. The current behavior is too limited.

like image 326
TheFogger Avatar asked Aug 07 '11 14:08

TheFogger


3 Answers

you can change this behaviour of windows programmatically by calling SetConsoleHistoryInfo with a correctly setup CONSOLE_HISTORY_INFO structure... there seems to be no managed class/method so you will have to use DllImport etc.

http://msdn.microsoft.com/en-us/library/ms686031%28v=VS.85%29.aspx
http://msdn.microsoft.com/en-us/library/ms682077%28v=VS.85%29.aspx

IF need be - several other aspects of the console can be handled in a managed way - see c# console, Console.Clear problem

like image 85
Yahia Avatar answered Sep 22 '22 16:09

Yahia


The history feature is built into the Windows Command shell, it is not a feature of your application. AFAIK there's no way to disable this in your code as it's specific to the Windows Shell Environment (unless there's a setting that can be changed, which there probably is)

You could possibly override the default behavior by using a key listener to get all up arrow keypresses and execute your own code, that way the event doesn't drop down to the shell to handle.

like image 38
Jesus Ramos Avatar answered Sep 25 '22 16:09

Jesus Ramos


Yes, this is a feature of the console subsystem, not your application. To change it, click the console's control box (top left), properties, options tab: "Command history." The default is 50 items, 4 buffers. Supposedly this can be configured programmatically with DOSKEY from the command line, but a few minutes tinkering didn't lead me anywhere.

ALT+F7 will clear the command history, as will executing the command DOSKEY /reinstall. I tested in Windows 7.

Update: The corresponding Win32 API call is SetConsoleHistoryInfo and the p/invoke signature can be found at http://pinvoke.net/default.aspx/kernel32/SetConsoleHistoryInfo.html

like image 42
x0n Avatar answered Sep 25 '22 16:09

x0n