Console application. Console.Clear = IOException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dinsdale

    Console application. Console.Clear = IOException

    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.IOExc eption: The handle is invalid.

    at System.IO.IO__E rror.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
  • Dinsdale

    #2
    Re: Console application. Console.Clear = IOException

    On Feb 28, 2:51 pm, Dinsdale <russ.ha...@gma il.comwrote:
    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.IOExc eption: The handle is invalid.
    >
    at System.IO.IO__E rror.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???

    Comment

    • Tom Shelton

      #3
      Re: Console application. Console.Clear = IOException

      On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gma il.comwrote:
      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.IOExc eption: The handle is invalid.
      >
      at System.IO.IO__E rror.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

      Comment

      • Michael D. Ober

        #4
        Re: Console application. Console.Clear = IOException

        "Tom Shelton" <tom_shelton@co mcast.netwrote in message
        news:2bcea0a4-d0fd-4d85-aa26-4c5e8b35c152@e3 9g2000hsf.googl egroups.com...
        On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gma il.comwrote:
        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.IOExc eption: The handle is invalid.
        >
        at System.IO.IO__E rror.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

        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.


        Comment

        • Tom Shelton

          #5
          Re: Console application. Console.Clear = IOException

          On Mar 11, 7:26 pm, "Michael D. Ober" <obermd.@.alum. mit.edu.nospam. >
          wrote:
          "Tom Shelton" <tom_shel...@co mcast.netwrote in message
          >
          news:2bcea0a4-d0fd-4d85-aa26-4c5e8b35c152@e3 9g2000hsf.googl egroups.com...
          On Feb 28, 1:51 pm, Dinsdale <russ.ha...@gma il.comwrote:
          >
          >
          >
          >
          >
          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.IOExc eption: The handle is invalid.
          >
          at System.IO.IO__E rror.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 PeekConsoleInpu t
          trick... This may actually be what the OP needs, because he also
          wants to not display the prompt....

          --
          Tom Shelton

          Comment

          Working...