How can a C# Windows Console application tell if it is run interactively

Jeff Leonard picture Jeff Leonard · Jul 27, 2009 · Viewed 14.9k times · Source

How can a Windows console application written in C# determine whether it is invoked in a non-interactive environment (e.g. from a service or as a scheduled task) or from an environment capable of user-interaction (e.g. Command Prompt or PowerShell)?

Answer

Glenn Slayden picture Glenn Slayden · Jan 3, 2012

To determine if a .NET application is running in GUI mode:

bool is_console_app = Console.OpenStandardInput(1) != Stream.Null;