Check if Input, Output and Error is redirected on the Console or not in C#


To check if the input is redirected on the Console or not, the code is as follows −

Example

 Live Demo

using System;
public class Demo{
   public static void Main(string[] args){
      Console.WriteLine("Input Redirected? = "+Console.IsInputRedirected);
   }
}

Output

This will produce the following output −

Input Redirected? = False

Example

To check if the output is redirected on the Console or not, the code is as follows −

 Live Demo

using System;
public class Demo{
   public static void Main(string[] args){
      Console.WriteLine("Output Redirected? = "+Console.IsInputRedirected);
   }
}

Output

This will produce the following output −

Output Redirected? = False

Example

To check if the error is redirected on the Console or not, the code is as follows −

 Live Demo

using System;
public class Demo{
   public static void Main(string[] args){
      Console.WriteLine("Error Redirected on Console? = "+Console.IsErrorRedirected);
   }
}

Output

This will produce the following output −

Error Redirected on Console? = True

Updated on: 11-Dec-2019

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements