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