Why does my console application have command histo

2019-04-18 04:24发布

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.

4条回答
闹够了就滚
2楼-- · 2019-04-18 04:34

Not tested, but it looks like passing an instance of CONSOLE_HISTORY_INFO to SetConsoleHistoryInfo with buffer size and count set to 1 would give the same control as the console window properties dialogue.

P/Invoke definitions at pinvoke.net

Also note this requires Windows V6 or later (ie. Vista/2008/7/2008R2).

查看更多
Summer. ? 凉城
3楼-- · 2019-04-18 04:44

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

查看更多
做自己的国王
4楼-- · 2019-04-18 04:45

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

查看更多
家丑人穷心不美
5楼-- · 2019-04-18 04:55

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.

查看更多
登录 后发表回答