Is is possible to programmatically clear the conso

2019-06-16 03:25发布

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.

6条回答
我命由我不由天
2楼-- · 2019-06-16 03:29

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

查看更多
做个烂人
3楼-- · 2019-06-16 03:39

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"
查看更多
男人必须洒脱
4楼-- · 2019-06-16 03:42

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

查看更多
家丑人穷心不美
5楼-- · 2019-06-16 03:42

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

查看更多
你好瞎i
6楼-- · 2019-06-16 03:45

This code is working for me to clear the command history. Limited testing and no guarantees on portability.

public static partial class ConsoleEx
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetConsoleHistoryInfo(CONSOLE_HISTORY_INFO ConsoleHistoryInfo);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool GetConsoleHistoryInfo(CONSOLE_HISTORY_INFO ConsoleHistoryInfo);

    [StructLayout(LayoutKind.Sequential)]
    private class CONSOLE_HISTORY_INFO
    {
        public uint cbSize;
        public uint BufferSize;
        public uint BufferCount;
        public uint TrimDuplicates;
    }

    public static void ClearConsoleHistory()
    {
        var chi = new CONSOLE_HISTORY_INFO();
        chi.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(CONSOLE_HISTORY_INFO));

        if (!GetConsoleHistoryInfo(chi))
        {
            return;
        }

        var originalBufferSize = chi.BufferSize;
        chi.BufferSize = 0;

        if (!SetConsoleHistoryInfo(chi))
        {
            return;
        }

        chi.BufferSize = originalBufferSize;

        if (!SetConsoleHistoryInfo(chi))
        {
            return;
        }
    }
}
查看更多
够拽才男人
7楼-- · 2019-06-16 03:55

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.

查看更多
登录 后发表回答