Microsoft KB Archive/319257

= How to clear the Console window programmatically with Visual C# =

Article ID: 319257

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q319257



For a Microsoft Visual Basic version of this article, see 319239.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System
 * System.Runtime.InteropServices

IN THIS TASK
SUMMARY
 * Sample Program

REFERENCES



SUMMARY
This step-by-step article demonstrates how to clear the Console window programmatically by using Visual C#.

back to the top

Sample Program
 Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. Create a new Visual C# Console Application project.  Paste the following using statement at the top of the default class: using nsClearConsole;   Paste the following code in the Main procedure of the Console application: static void Main(string[] args) {  ClearConsole ClearMyConsole = new ClearConsole; Console.WriteLine(&quot;THIS IS FIRST LINE&quot;); // Some text Console.WriteLine(&quot;THIS IS SECOND LINE&quot;); // Some text Console.WriteLine(&quot;THIS IS THIRD LINE&quot;); // Some text Console.WriteLine(&quot;THIS IS FOURTH LINE&quot;); // Some text Console.WriteLine(&quot;THIS IS FIFTH LINE&quot;); // Some text Console.WriteLine(&quot;Hit Enter to Clear&quot;); // Some text Console.ReadLine; // Wait for user input ClearMyConsole.Clear; // Clear the screen Console.WriteLine(&quot;THE CONSOLE WAS CLEARED&quot;); // Some text to clear console Console.WriteLine(&quot;Hit Enter to Terminate&quot;); //Some text Console.ReadLine; // Wait for user input }                    On the Project menu, click Add Class. In the Add New Item dialog box, select Class, and then click Open.  Replace all of the existing code in the new class with the following code: using System; using System.Runtime.InteropServices;

namespace nsClearConsole {     ///       /// Summary description for ClearConsole. /// <BR/> public class ClearConsole {             private const int STD_OUTPUT_HANDLE  = -11; private const byte EMPTY = 32;

[StructLayout(LayoutKind.Sequential)] struct COORD {           public short x;            public short y;         }

[StructLayout(LayoutKind.Sequential)] struct SMALL_RECT {           public short Left; public short Top; public short Right; public short Bottom; }        [StructLayout(LayoutKind.Sequential)] struct CONSOLE_SCREEN_BUFFER_INFO {           public COORD dwSize; public COORD dwCursorPosition; public int wAttributes; public SMALL_RECT srWindow; public COORD dwMaximumWindowSize; }

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;GetStdHandle&quot;, SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] private static extern int GetStdHandle(int nStdHandle);

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;FillConsoleOutputCharacter&quot;, SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] private static extern int FillConsoleOutputCharacter(int hConsoleOutput, byte cCharacter, int nLength, COORD dwWriteCoord, ref int lpNumberOfCharsWritten);

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;GetConsoleScreenBufferInfo&quot;, SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] private static extern int GetConsoleScreenBufferInfo(int hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);

[DllImport(&quot;kernel32.dll&quot;, EntryPoint=&quot;SetConsoleCursorPosition&quot;, SetLastError=true, CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)] private static extern int SetConsoleCursorPosition(int hConsoleOutput, COORD dwCursorPosition);

private int hConsoleHandle;

public ClearConsole {           //             // TODO: Add constructor logic here. //            hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); }

public void Clear {           int hWrittenChars = 0; CONSOLE_SCREEN_BUFFER_INFO strConsoleInfo = new CONSOLE_SCREEN_BUFFER_INFO; COORD Home; Home.x = Home.y = 0; GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo); FillConsoleOutputCharacter(hConsoleHandle, EMPTY, strConsoleInfo.dwSize.x * strConsoleInfo.dwSize.y, Home, ref hWrittenChars); SetConsoleCursorPosition(hConsoleHandle, Home); }     }   }

</li> Press F5 to run the application. Note that the text in the Console window is cleared.</li></ol>

back to the top

<div class="references_section">