.NET Process does not get along with .NET console
-
So I am not sure if this is my "worst practices" or MS'. If you start a process (console app) which makes a call to Console.WindowWidth from C# (3.5) Process object, it crashes. I guess, I'm removing my fancy console screen formatting--for automated system testing, at least.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "DummyTest.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.Start(); //<-------<< causes System.IOException because of Console.WindowWidth
namespace DummyTest
{
class Program
{
static void Main(string[] args)
{Console.WriteLine("Hello world! My window is this wide:"); Console.WriteLine(Console.WindowWidth.ToString()); } }
}
-
So I am not sure if this is my "worst practices" or MS'. If you start a process (console app) which makes a call to Console.WindowWidth from C# (3.5) Process object, it crashes. I guess, I'm removing my fancy console screen formatting--for automated system testing, at least.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "DummyTest.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.Start(); //<-------<< causes System.IOException because of Console.WindowWidth
namespace DummyTest
{
class Program
{
static void Main(string[] args)
{Console.WriteLine("Hello world! My window is this wide:"); Console.WriteLine(Console.WindowWidth.ToString()); } }
}
That makes sense because you're piping its IO (
.RedirectStandardXxx = true
), so it is not connected to the console, just your starting program. I have confirmed this by replicating your exception (in VB.NET) and then only changing to.RedirectStandardOutput = False
and there is then no error, but it may of course not be the functionality you need.
Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)
-
So I am not sure if this is my "worst practices" or MS'. If you start a process (console app) which makes a call to Console.WindowWidth from C# (3.5) Process object, it crashes. I guess, I'm removing my fancy console screen formatting--for automated system testing, at least.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "DummyTest.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.Start(); //<-------<< causes System.IOException because of Console.WindowWidth
namespace DummyTest
{
class Program
{
static void Main(string[] args)
{Console.WriteLine("Hello world! My window is this wide:"); Console.WriteLine(Console.WindowWidth.ToString()); } }
}
-
That makes sense because you're piping its IO (
.RedirectStandardXxx = true
), so it is not connected to the console, just your starting program. I have confirmed this by replicating your exception (in VB.NET) and then only changing to.RedirectStandardOutput = False
and there is then no error, but it may of course not be the functionality you need.
Regards, Mark Hurd, B.Sc.(Ma.) (Hons.)
Ahh... and it is by design. A little further reading indicates that when redirecting io the underlying io and error streams Console object has NOTHING to do with the console screen that might be displayed albeit blank. This was lost on me initially.
Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO..::.IOException if you redirect a standard stream.
http://msdn.microsoft.com/en-us/library/system.console.aspx[^]