Clear the Colsole Window

  • Thread starter Thread starter Josh
  • Start date Start date
J

Josh

Hi Guys,

I have written a console application in C#. I wish to printout a percentage
to the screen as the program is running.

Does anyone know how to clear the console window?
or does anyone konw how to do this?

Thanks
Josh
 
Josh said:
Hi Guys,

I have written a console application in C#. I wish to printout a
percentage to the screen as the program is running.

Does anyone know how to clear the console window?
or does anyone konw how to do this?

Here's some links to look at:

http://support.microsoft.com/kb/q99261/

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp

But in general it's better to stream information to the console. If you
need to show a progress bar, a winform is the right tool for the job.

David
 
Hi,

For displaying progress you can "hack" it by sending a carriage return (\r)
without new line (\n) thus overwriting the old value. Example:

static void Main(string[] args)
{
for(int i = 0; i < 100; i+=5)
{
Console.Write("\rProgress: {0}%\t\t\t", i);
System.Threading.Thread.Sleep(300);
}
Console.WriteLine("\rFinished\t\t\t\t\t");
Console.ReadLine();
}

For clearing completely the console window see:

http://support.microsoft.com/?kbid=319257

Hope this helps
Martin Dechev
ASP.NET MVP
 
Back
Top