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#?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can find out by p/invoking the Windows FileType() API function. Here’s a helper class:
Usage:
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