Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the Console in c#.net?

Tags:

c#

console

wpf

Just that. I found a similar question here : c# console, Console.Clear problem

but that didn't answer the question.

UPDATES :

Console.Clear() throws : IOException (The handle is invalid)

The app is a WPF app. Writing to the console however is no problem at all, nor is reading from.

like image 444
Peter Avatar asked Apr 19 '09 21:04

Peter


People also ask

What is Clrscr () in C?

What is Clrscr() in C? clrscr() function clears the screen and moves the cursor to the upper-left-hand corner of the screen. It is defined in conio. h header file. This function is optional.

How do I clear the console in Terminal?

A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.

Which code is used to clear the Consol?

clear() The console. clear() method clears the console if the console allows it.


2 Answers

Console.Clear() works in a console application.

When Calling Console.Clear() in a ASP.NET web site project or in a windows forms application, then you'll get the IOException.

What kind of application do you have?

Update:

I'm not sure if this will help, but as you can read in this forum thread, Console.Clear() throws an IOException if the console output is being redirected. Maybe this is the case for WPF applications? The article describes how to check whether the console is being redirected.

like image 178
M4N Avatar answered Nov 15 '22 09:11

M4N


Try

Console.Clear();

EDIT

Are you trying this method on a non-Console application? If so that would explain the error. Other types of applications, ASP.Net projects, WinForms, etc ... don't actually create a console for writing. So the Clear has nothing to operate on and throws an exception.

like image 29
JaredPar Avatar answered Nov 15 '22 09:11

JaredPar