Console application. Console.Clear = IOException

  • Thread starter Thread starter Dinsdale
  • Start date Start date
D

Dinsdale

I am writing a small console application that runs fine unless I am re-
directing the output to a file (i.e. c:\ > app.exe >>output.txt) . I
have determined that the issue is caused by the Console.Clear()
function. When piping to a file, the console.clear causes the
following error:

Unhandled Exception: System.IO.IOException: The handle is invalid.

at System.IO.IO__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.Console.Clear()
....

Is there any way to determine where my output is being directed so
that I can skip over the Console.Clear command if I am re-directing
stdOut?

The other side of the error is with input. I prompt the user at one or
two points but if the output is re-directed, I would like to simply
skip the prompt. Again, this would be possible if I can determine
where standard output is going.

Thanks!
Dinsdale
 
I am writing a small console application that runs fine unless I am re-
directing the output to a file (i.e. c:\ > app.exe >>output.txt) . I
have determined that the issue is caused by the Console.Clear()
function. When piping to a file, the console.clear causes the
following error:

Unhandled Exception: System.IO.IOException: The handle is invalid.

at System.IO.IO__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.Console.Clear()
...

Is there any way to determine where my output is being directed so
that I can skip over the Console.Clear command if I am re-directing
stdOut?

The other side of the error is with input. I prompt the user at one or
two points but if the output is re-directed, I would like to simply
skip the prompt. Again, this would be possible if I can determine
where standard output is going.

Thanks!
Dinsdale

So did nobody answer this because I am stupid and totally missed
something obvious? Seriously, hints anyone???
 
I am writing a small console application that runs fine unless I am re-
directing the output to a file (i.e. c:\ > app.exe >>output.txt) . I
have determined that the issue is caused by the Console.Clear()
function. When piping to a file, the console.clear causes the
following error:

Unhandled Exception: System.IO.IOException: The handle is invalid.

at System.IO.IO__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.Console.Clear()
...

Is there any way to determine where my output is being directed so
that I can skip over the Console.Clear command if I am re-directing
stdOut?

The other side of the error is with input. I prompt the user at one or
two points but if the output is re-directed, I would like to simply
skip the prompt. Again, this would be possible if I can determine
where standard output is going.

Thanks!
Dinsdale

AFIK, there is no way to tell if you have been redirected. Sorry.
 
I am writing a small console application that runs fine unless I am re-
directing the output to a file (i.e. c:\ > app.exe >>output.txt) . I
have determined that the issue is caused by the Console.Clear()
function. When piping to a file, the console.clear causes the
following error:

Unhandled Exception: System.IO.IOException: The handle is invalid.

at System.IO.IO__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.Console.Clear()
...

Is there any way to determine where my output is being directed so
that I can skip over the Console.Clear command if I am re-directing
stdOut?

The other side of the error is with input. I prompt the user at one or
two points but if the output is re-directed, I would like to simply
skip the prompt. Again, this would be possible if I can determine
where standard output is going.

Thanks!
Dinsdale

You can't do it directly from the framework. However, take a look at
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnaraskdr/html/askgui07152003.asp
for information detecting redirection. Personally, I would wrap my
Console.Clear() function inside a small sub as follows:

public Sub ClearConsole()
static failed as boolean = false
if failed then exit sub

try
system.Console.Clear()
catch
failed = true
end try
end sub

Mike.
 
You can't do it directly from the framework.  However, take a look athttp://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnar...


Hey, Michael - good find. I wasn't aware of the PeekConsoleInput
trick... This may actually be what the OP needs, because he also
wants to not display the prompt....
 
Hey, Michael - good find. I wasn't aware of the PeekConsoleInput
trick... This may actually be what the OP needs, because he also
wants to not display the prompt....

Thanks Michael, this is the type of info I was looking for. I kind of
assumed I would have to delve into some win32 / platform sdk stuff.
Another good article can be found here: http://msdn2.microsoft.com/en-us/library/ms682075.aspx
I found this while research the Dr. Gui article you referenced. Also a
reference file here: http://msdn2.microsoft.com/en-us/library/ms682073(VS.85).aspx.
I'll try to remember to post an answer if I find one as the Dr.Gui
example deals with input, not output...

Cheers,
Dinsdale
 
Thanks Michael, this is the type of info I was looking for. I kind of
assumed I would have to delve into some win32 / platform sdk stuff.
Another good article can be found here:http://msdn2.microsoft.com/en-us/library/ms682075.aspx
I found this while research the Dr. Gui article you referenced. Also a
reference file here:http://msdn2.microsoft.com/en-us/library/ms682073(VS.85).aspx.
I'll try to remember to post an answer if I find one as the Dr.Gui
example deals with input, not output...

Cheers,
Dinsdale

Okay, here's what I have so far:

I can use the GetStdHandle sdk call in VB.net to return the handle of
the current console window (code below). In the preliminary tests, I
find that the IntPtr returns 7 if the handle is pointing to the
console window, and a value other than 7 if it is pointing to a file
(i.e. 132, 128 etc). What I do not know for sure is if the console
window ALWAYS returns 7? This seems way too easy for the level of
effort I have had to invest in finding this solution... Can anyone
confirm or deny this (return value of 7, not my effot level. lol)?

Code:

Imports System
Imports System.Runtime.InteropServices

Module Module1

Sub Main()

Dim handle As IntPtr

handle = STDIO.GetStdHandle(STDIO.STD_OUTPUT_HANDLE)

If handle <> 7 Then
Console.WriteLine("This is a redirected console. Handle # " +
handle.ToString())
Else
Console.WriteLine("Not redirected")
Console.ReadLine()
End If

End Sub

End Module

Class STDIO
' see http://msdn2.microsoft.com/en-us/library/ms683231(VS.85).aspx
Public Const STD_OUTPUT_HANDLE As Long = (-11 And &HFFFFFFFFL)

<DllImport("Kernel32", SetLastError:=True)> _
Public Shared Function GetStdHandle(ByVal nStdHandle As
System.UInt32) _
As IntPtr
End Function

End Class


Cheers,
Dinsdale
 
Back
Top