Determine if .Net core console application is running in user interactive mode
The problem that .NET Core faces more than classic .NET Framework is how "user interactive" is defined. Even on "classic" .NET, the Environment.UserInteractive
feels hacky as it relies on a system API call to query user object flags and tests for a flag described as Window station has visible display surfaces
. It is unclear what exactly this should semantically mean on a GUI-less windows nano server that you use to run commands from.
My suggestion is to determine the exact use case on what you want to test. You could for instance test if the attached input and output streams are redirected using Console.IsOutputRedirected
and Console. IsInputRedirected
. On non-windows systems, a call to isatty()
could be made but that isn't currently available as .NET API (you'd have to write the PInvoke code). If you want to determine if you are running as a windows service, TopShelf checks if the process has been started by the service host. Another approach would be to add additional arguments for specific use cases - e.g. add and check for --noninteractive
when you want to run a tool from a script.
To complement Martin Ullrich's helpful answer:
If you're willing to define user-interactive as runs in a console/terminal window visible to the current user, you can use the following approach (C#), which should work on all supported platforms:
Note: Visible means visible in principle, i.e., can be made visible by the user if currently obscured or minimized.
using System;
using System.Runtime.InteropServices;
namespace net.same2u.pg
{
static class Program
{
// P/Invoke declarations for Windows.
[DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr hWnd);
// Indicates if the current process is running:
// * on Windows: in a console window visible to the user.
// * on Unix-like platform: in a terminal window, which is assumed to imply
// user-visibility (given that hidden processes don't need terminals).
public static bool HaveVisibleConsole()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
IsWindowVisible(GetConsoleWindow())
:
Console.WindowHeight > 0;
}
static void Main(string[] args)
{
Console.WriteLine($"Running in visible console? {HaveVisibleConsole()}");
}
}
}
Note:
On Windows, all console-mode applications always run in a console window, whether that window is visible or not; the P/Invoke function declarations check if the console window (
GetConsoleWindow()
) - if any - associated with the current process is a visible window (IsWindowVisible()
, on the current user's desktop).On Unix-like platforms, non-GUI applications fundamentally don't need a terminal window to run, and launching such an application from a GUI app, for instance, does not involve a terminal. Therefore, the assumption is that if a terminal window is present at all, it implies that that window is visible;
Console.WindowHeight
only contains a positive value if a terminal window is present.