Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:22:48+00:00 2026-05-16T10:22:48+00:00

I want to write a console application that have a different behavior depending if

  • 0

I want to write a console application that have a different behavior depending if the input is coming from keyboard or from, say, a file.

Is it possible? What’s the most elegant way to do it in C#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:22:49+00:00Added an answer on May 16, 2026 at 10:22 am

    You can find out by p/invoking the Windows FileType() API function. Here’s a helper class:

    using System;
    using System.Runtime.InteropServices;
    
    public static class ConsoleEx {
        public static bool IsOutputRedirected {
            get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdout)); }
        }
        public static bool IsInputRedirected {
            get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stdin)); }
        }
        public static bool IsErrorRedirected {
            get { return FileType.Char != GetFileType(GetStdHandle(StdHandle.Stderr)); }
        }
    
        // P/Invoke:
        private enum FileType { Unknown, Disk, Char, Pipe };
        private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
        [DllImport("kernel32.dll")]
        private static extern FileType GetFileType(IntPtr hdl);
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(StdHandle std);
    }
    

    Usage:

    bool inputRedirected = ConsoleEx.IsInputRedirected;
    

    UPDATE: these methods were added to the Console class in .NET 4.5. Without attribution I might add 🙁 Simply use the corresponding method instead of this helper class.

    https://msdn.microsoft.com/en-us/library/system.console.isoutputredirected.aspx
    https://msdn.microsoft.com/en-us/library/system.console.isinputredirected.aspx
    https://msdn.microsoft.com/en-us/library/system.console.iserrorredirected.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer AFAIK references are not subject to race condition because the… May 17, 2026 at 1:37 am
  • Editorial Team
    Editorial Team added an answer Its the destructor! In simple terms a destructor is a… May 17, 2026 at 1:37 am
  • Editorial Team
    Editorial Team added an answer You could just buy a few clearance 42" tvs which… May 17, 2026 at 1:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a long-running console-based application Sender that sends simple text to STDOUT using
If I would want to create a c# application that support multiple languages, how
My web application returns a file from the filesystem. These files are dynamic, so
I am building a console application that gathers some data and then sends it
I have a Rails 3 application with a User class, and a tab-delimited file
Oh, 2 things: 1) It is a console application. 2 ) I know it
I have two C# applications, one is reading a file (File A) line by
I have an application which has a loop, part of a Scheduler, which runs
I have a windows service that generates logs as it does some execution. I
So say I run my program: Process proc = new Process(); proc.StartInfo.FileName = Path.GetDirectoryName(Application.ExecutablePath)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.