确定 .Net core 控制台应用程序是否在用户交互模式下运行 [英] Determine if .Net core console application is running in user interactive mode

查看:28
本文介绍了确定 .Net core 控制台应用程序是否在用户交互模式下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以确定 .Net 核心控制台应用程序是否在用户交互模式下运行?

Is it possible to determine if a .Net core console application is running in user interactive mode?

在以前的 .Net 版本中,可以测试 Environment.UserInteractive 以查看用户是否有权访问应用程序.这在 .Net 核心中似乎不存在.

In previous versions of .Net it was possible to test Environment.UserInteractive to see if the user had access to the application. This doesn't seem to exist in .Net core.

推荐答案

.NET Core 比经典的 .NET Framework 面临更多的问题是如何定义用户交互".即使在经典"的 .NET 上,Environment.UserInteractive 也感觉很古怪,因为它依赖于 系统 API 调用以查询用户对象标志 并测试描述为 Window station 有可见的显示面.目前尚不清楚这在用于运行命令的无 GUI 的 Windows nano 服务器上在语义上究竟意味着什么.

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.

我的建议是确定您要测试的确切用例.例如,您可以测试附加的输入和输出流是否使用 Console.IsOutputRedirectedConsole 重定向.IsInputRedirected.在非 Windows 系统上,可以调用 isatty() 但目前还不能作为 .NET API 使用(您必须编写 PInvoke 代码).如果要确定是否作为 Windows 服务运行,TopShelf 检查进程是否已被服务主机启动.另一种方法是为特定用例添加额外的参数 - 例如当您想从脚本运行工具时,添加并检查 --noninteractive.

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.

这篇关于确定 .Net core 控制台应用程序是否在用户交互模式下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆