PC Review


Reply
Thread Tools Rate Thread

Console application. Console.Clear = IOException

 
 
Dinsdale
Guest
Posts: n/a
 
      28th Feb 2008
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
 
Reply With Quote
 
 
 
 
Dinsdale
Guest
Posts: n/a
 
      11th Mar 2008
On Feb 28, 2:51 pm, Dinsdale <russ.ha...@gmail.com> wrote:
> 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???
 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      12th Mar 2008
On Feb 28, 1:51*pm, Dinsdale <russ.ha...@gmail.com> wrote:
> 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.

--
Tom Shelton
 
Reply With Quote
 
Michael D. Ober
Guest
Posts: n/a
 
      12th Mar 2008
"Tom Shelton" <(E-Mail Removed)> wrote in message
news:2bcea0a4-d0fd-4d85-aa26-(E-Mail Removed)...
On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gmail.com> wrote:
> 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/de...ui07152003.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.


 
Reply With Quote
 
Tom Shelton
Guest
Posts: n/a
 
      12th Mar 2008
On Mar 11, 7:26*pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>
wrote:
> "Tom Shelton" <tom_shel...@comcast.net> wrote in message
>
> news:2bcea0a4-d0fd-4d85-aa26-(E-Mail Removed)...
> On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gmail.com> wrote:
>
>
>
>
>
> > 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 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....

--
Tom Shelton
 
Reply With Quote
 
Dinsdale
Guest
Posts: n/a
 
      12th Mar 2008
On Mar 11, 9:35 pm, Tom Shelton <tom_shel...@comcast.net> wrote:
> On Mar 11, 7:26 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>
> wrote:
>
>
>
> > "Tom Shelton" <tom_shel...@comcast.net> wrote in message

>
> >news:2bcea0a4-d0fd-4d85-aa26-(E-Mail Removed)...
> > On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gmail.com> wrote:

>
> > > 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 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....
>
> --
> Tom Shelton


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
 
Reply With Quote
 
Dinsdale
Guest
Posts: n/a
 
      14th Mar 2008
On Mar 12, 11:28 am, Dinsdale <russ.ha...@gmail.com> wrote:
> On Mar 11, 9:35 pm, Tom Shelton <tom_shel...@comcast.net> wrote:
>
>
>
> > On Mar 11, 7:26 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam.>
> > wrote:

>
> > > "Tom Shelton" <tom_shel...@comcast.net> wrote in message

>
> > >news:2bcea0a4-d0fd-4d85-aa26-(E-Mail Removed)...
> > > On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gmail.com> wrote:

>
> > > > 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 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....

>
> > --
> > Tom Shelton

>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vb.net Console.OutputEncoding = Unicode : IOException lambertb@gmail.com Microsoft Dot NET 0 22nd Feb 2006 04:08 PM
vb.net Console.OutputEncoding = Unicode : IOException lambertb@gmail.com Microsoft VB .NET 0 22nd Feb 2006 04:08 PM
How to run a console application without showing the console screen? Tee Microsoft C# .NET 2 11th Feb 2005 06:22 AM
How to run a console application without showing the console screen? Tee Microsoft VB .NET 2 11th Feb 2005 06:22 AM
How to set a fixed line in a console application liek UNIX console AA Microsoft C# .NET 1 6th Dec 2003 02:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:54 AM.