Bug 12752 - Console.IsInputRedirected() and friends return false if the stream is redirected to NUL
Summary: Console.IsInputRedirected() and friends return false if the stream is redirec...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 2.10.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-06-18 22:56 UTC by Greg Najda
Modified: 2013-07-21 02:07 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Greg Najda 2013-06-18 22:56:59 UTC
Test program at bottom of this report.

C:\Users\Greg\Documents\Visual Studio 2012\Projects\IsRedirectedTest\IsRedirectedTest\bin\Debug>IsRedirectedTest.exe < NUL
Input redirected: True
Output redirected: False
Error redirected: False

C:\Users\Greg\Documents\Visual Studio 2012\Projects\IsRedirectedTest\IsRedirectedTest\bin\Debug>"C:\Program Files (x86)\Mono-3.0.10\bin\mono.exe" IsRedirectedTest.exe < NUL
Input redirected: False
Output redirected: False
Error redirected: False

Similar behavior can be observed for IsOutputRedirected and IsErrorRedirected. Proposed fix: Change ves_icall_System_ConsoleDriver_Isatty in mono/metadata/console-win32.c to

MonoBoolean
ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
{
	MONO_ARCH_SAVE_REGS;

	DWORD mode;
	return GetConsoleMode (handle, &mode) != 0;
}

instead of

MonoBoolean
ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
{
	MONO_ARCH_SAVE_REGS;

	return (GetFileType (handle) == FILE_TYPE_CHAR);
}


using System;
using System.IO;

namespace IsRedirectedTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamWriter output = new StreamWriter("output.txt"))
            {
                Console.WriteLine("Input redirected: {0}", Console.IsInputRedirected);
                output.WriteLine("Input redirected: {0}", Console.IsInputRedirected);
                Console.WriteLine("Output redirected: {0}", Console.IsOutputRedirected);
                output.WriteLine("Output redirected: {0}", Console.IsOutputRedirected);
                Console.WriteLine("Error redirected: {0}", Console.IsErrorRedirected);
                output.WriteLine("Error redirected: {0}", Console.IsErrorRedirected);
            }
        }
    }
}