Wednesday, November 28, 2007

how to detect if standard out is redirected

so i spent a dumb amount of time trying to figure this out, starting by trying to tweak the Dr. Gui method for detecting a redirected stdin, and finally settled on a pretty gritty solution -- Console.Clear() throws an exception if stdout is redirected.

bool consoleIsRedirected = false;
try
{
Console.Clear();
}
catch
{
consoleIsRedirected = true;
}


this has some obvious drawbacks that i don't even want to try to defend, but it works as a solution until the right one shows up.