private static Stream GetStandardFile(int handleType, FileAccess access)
{
IntPtr handle = Interop.Kernel32.GetStdHandle(handleType);
// If someone launches a managed process via CreateProcess, stdout,
// stderr, & stdin could independently be set to INVALID_HANDLE_VALUE.
// Additionally they might use 0 as an invalid handle. We also need to
// ensure that if the handle is meant to be writable it actually is.
if (handle == IntPtr.Zero || handle == s_InvalidHandleValue ||
(access != FileAccess.Read && !ConsoleHandleIsWritable(handle)))
{
return Stream.Null;
}
return new WindowsConsoleStream(handle, access, GetUseFileAPIs(handleType));
}