如何确定控制台应用程序是如何启动的?

发布于 2024-09-15 04:16:59 字数 78 浏览 2 评论 0原文

如何判断用户是否通过双击 EXE(或快捷方式)启动了我的控制台应用程序,或者他们是否已经打开了命令行窗口并在该会话中执行了我的控制台应用程序?

How can I tell whether the user launched my console application by double-clicking the EXE (or a shortcut), or whether they already had a command line window open and executed my console app within that session?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云巢 2024-09-22 04:16:59

将此静态字段粘贴到您的“Program”类中,以确保它在任何输出之前运行:

static bool StartedFromGui = 
         !Console.IsOutputRedirected
      && !Console.IsInputRedirected
      && !Console.IsErrorRedirected
      && Environment.UserInteractive
      && Environment.CurrentDirectory == System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
      && Console.CursorTop == 0 && Console.CursorLeft == 0
      && Console.Title == Environment.GetCommandLineArgs()[0]
      && Environment.GetCommandLineArgs()[0] == System.Reflection.Assembly.GetEntryAssembly().Location;

这有点矫枉过正/偏执,但会从资源管理器启动,而不响应诸如 之类的事情cls&& app.exe(通过检查完整路径)甚至cls && “f:\ull\path\to\app.exe”(通过查看标题)。

我从 这个问题的 win32 版本中得到了这个想法< /a>.

Stick this static field in your "Program" class to ensure it runs before any output:

static bool StartedFromGui = 
         !Console.IsOutputRedirected
      && !Console.IsInputRedirected
      && !Console.IsErrorRedirected
      && Environment.UserInteractive
      && Environment.CurrentDirectory == System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
      && Console.CursorTop == 0 && Console.CursorLeft == 0
      && Console.Title == Environment.GetCommandLineArgs()[0]
      && Environment.GetCommandLineArgs()[0] == System.Reflection.Assembly.GetEntryAssembly().Location;

This is a little bit overkill/paranoid, but picks up being started from Explorer while not responding to things like cls && app.exe (by checking for the full path) or even cls && "f:\ull\path\to\app.exe" (by looking at the title).

I got the idea from the win32 version of this question.

小伙你站住 2024-09-22 04:16:59

您也许可以通过 P/Invoking Win32 GetStartupInfo() 函数。

[DllImport("kernel32", CharSet=CharSet.Auto)]
internal static extern void GetStartupInfo([In, Out] STARTUPINFO lpStartupInfo);

You might be able to figure it out by P/Invoking to the Win32 GetStartupInfo() function.

[DllImport("kernel32", CharSet=CharSet.Auto)]
internal static extern void GetStartupInfo([In, Out] STARTUPINFO lpStartupInfo);
┈┾☆殇 2024-09-22 04:16:59

你可以找出父进程是什么:

    Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess()?.Parent()?.ProcessName);

其中Parent()是一个扩展方法,例如:

public static class Extensions
{
    private static string FindIndexedProcessName(int pid)
    {
        var processName = Process.GetProcessById(pid).ProcessName;
        var processesByName = Process.GetProcessesByName(processName);
        string processIndexdName = null;

        for (var index = 0; index < processesByName.Length; index++)
        {
            processIndexdName = index == 0 ? processName : processName + "#" + index;
            var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
            if ((int)processId.NextValue() == pid)
            {
                return processIndexdName;
            }
        }

        return processIndexdName;
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
        return Process.GetProcessById((int)parentId.NextValue());
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}

You can find out what the parent process is:

    Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess()?.Parent()?.ProcessName);

Where Parent() is an extension method, e.g.:

public static class Extensions
{
    private static string FindIndexedProcessName(int pid)
    {
        var processName = Process.GetProcessById(pid).ProcessName;
        var processesByName = Process.GetProcessesByName(processName);
        string processIndexdName = null;

        for (var index = 0; index < processesByName.Length; index++)
        {
            processIndexdName = index == 0 ? processName : processName + "#" + index;
            var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
            if ((int)processId.NextValue() == pid)
            {
                return processIndexdName;
            }
        }

        return processIndexdName;
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
        return Process.GetProcessById((int)parentId.NextValue());
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文