C#Windows控制台应用程序如何判断它是否以交互方式运行

时间:2021-05-24 02:14:52

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)?

用C#编写的Windows控制台应用程序如何确定它是在非交互式环境中调用(例如从服务或作为计划任务)还是从能够进行用户交互的环境(例如命令提示符或PowerShell)调用?

4 个解决方案

#2


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

要确定.NET应用程序是否在GUI模式下运行:

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

#3


I haven't tested it, but Environment.UserInteractive looks promising.

我还没有测试过,但是Environment.UserInteractive看起来很有前景。

#4


If all you're trying to do is to determine whether the console will continue to exist after your program exits (so that you can, for example, prompt the user to hit Enter before the program exits), then all you have to do is to check if your process is the only one attached to the console. If it is, then the console will be destroyed when your process exits. If there are other processes attached to the console, then the console will continue to exist (because your program won't be the last one).

如果你要做的就是确定程序退出后控制台是否会继续存在(例如,你可以提示用户在程序退出前按Enter键),那么你所要做的就是检查您的进程是否是唯一连接到控制台的进程。如果是,那么当您的进程退出时,控制台将被销毁。如果控制台上附加了其他进程,则控制台将继续存在(因为您的程序不会是最后一个)。

For example*:

using System;
using System.Runtime.InteropServices;

namespace CheckIfConsoleWillBeDestroyedAtTheEnd
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // ...

            if (ConsoleWillBeDestroyedAtTheEnd())
            {
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey();
            }
        }

        private static bool ConsoleWillBeDestroyedAtTheEnd()
        {
            var processList = new uint[1];
            var processCount = GetConsoleProcessList(processList, 1);

            return processCount == 1;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint GetConsoleProcessList(uint[] processList, uint processCount);
    }
}

(*) Adapted from code found here.

(*)改编自此处的代码。

#1


#2


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

要确定.NET应用程序是否在GUI模式下运行:

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

#3


I haven't tested it, but Environment.UserInteractive looks promising.

我还没有测试过,但是Environment.UserInteractive看起来很有前景。

#4


If all you're trying to do is to determine whether the console will continue to exist after your program exits (so that you can, for example, prompt the user to hit Enter before the program exits), then all you have to do is to check if your process is the only one attached to the console. If it is, then the console will be destroyed when your process exits. If there are other processes attached to the console, then the console will continue to exist (because your program won't be the last one).

如果你要做的就是确定程序退出后控制台是否会继续存在(例如,你可以提示用户在程序退出前按Enter键),那么你所要做的就是检查您的进程是否是唯一连接到控制台的进程。如果是,那么当您的进程退出时,控制台将被销毁。如果控制台上附加了其他进程,则控制台将继续存在(因为您的程序不会是最后一个)。

For example*:

using System;
using System.Runtime.InteropServices;

namespace CheckIfConsoleWillBeDestroyedAtTheEnd
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            // ...

            if (ConsoleWillBeDestroyedAtTheEnd())
            {
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey();
            }
        }

        private static bool ConsoleWillBeDestroyedAtTheEnd()
        {
            var processList = new uint[1];
            var processCount = GetConsoleProcessList(processList, 1);

            return processCount == 1;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint GetConsoleProcessList(uint[] processList, uint processCount);
    }
}

(*) Adapted from code found here.

(*)改编自此处的代码。