Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console redirect with Console.CursorLeft [duplicate]

Tags:

c#

console

Possible Duplicate:
How can I determine whether Console.Out has been redirected to a file?
How to detect if Console.In (stdin) has been redirected?

Our system is currently using a CursorLeft function to write over the last thing that was printed onto the console window:

Console.CursorLeft = 0;

This is functionality that is required, so I cannot change it to just print a new line every time.

When redirecting the console to a text file, IOException occurs. How can I get the log file to either skip displaying these outputs or force it to write everyone of them out?

like image 595
Jastill Avatar asked Nov 12 '22 17:11

Jastill


1 Answers

It's quite obvious to me that the console is different from a file. For example you can use colors in console which will be lost when you redirect the output to a file. Thus it's not guaranteed that after redirection all console functionality can be preserved. I would suggest questioning that requirement because it doesn't seem feasible.

Anyways here is a similar problem based on the same Win32API underneath: "The handle is invalid" when running .net console via java

Here is the article from MSDN which has some information about how the console works (in the bottom of the page): http://msdn.microsoft.com/en-us/library/system.console.aspx

Another thing to think about is that although you may not be able to fix this problem you can definitely re-architect your application, so that you abstract the direct interaction with the console with some buffer class that accumulates all the changes you want to make to a single line (or several lines) in a virtual buffer and then only make a single call to Console.WriteLine when you know for sure that all the changes were done and that line isn't going to be changed anymore.

like image 121
Trident D'Gao Avatar answered Nov 24 '22 01:11

Trident D'Gao