c# - Windows 10 : How to determine whether a process is an App/Background Process/Windows Process

标签 c# windows windows-10-desktop

以编程方式,我如何确定 Windows 10 中的 3 个类别

  • 应用
  • 后台进程
  • Windows 服务

就像任务管理器一样?

即我需要一些 C# 代码,我可以确定应用程序列表与后台进程列表。检查 Win32_process 中的 executionState 不起作用。它始终为空。

谢谢!

enter image description here

最佳答案

原问题:

  • 如何检测正在运行的应用列表?

我对解决方案有不同的看法:

某些 UWP 应用程序具有主窗口标题。 检查主窗口标题不足以判断应用程序是否正在运行。

处于挂起状态的 UWP 应用程序仍将返回标题(见红色矩形)

enter image description here

所以为了检测挂起状态,我们需要覆盖

  1. 应用有标题但没有在前台运行
  2. 应用有标题但不在后台运行

{代码}

static void byProcess()
    {
        Process[] processes = Process.GetProcesses();
        List<string> suspendedAppsWhichHasTitle = new List<string>();

        foreach (var proc in processes)
        {
            // if it has main window title
            if (!string.IsNullOrEmpty(proc.MainWindowTitle))
            {
                // the app may be in suspend state 
                foreach (ProcessThread pT in proc.Threads)
                {
                    ThreadState ts = pT.ThreadState;
                    if (ts == ThreadState.Running)
                    {
                        suspendedAppsWhichHasTitle.Add(proc.MainWindowTitle);
                    }
                }                    
            }
        }

        foreach(string app in suspendedAppsWhichHasTitle)
        {
            Console.WriteLine(app);
        }

        if (suspendedAppsWhichHasTitle.Count == 0)
        {
            Console.WriteLine("No visible app is running!");
        }
        Console.Read();
    }

关于c# - Windows 10 : How to determine whether a process is an App/Background Process/Windows Process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615279/

相关文章:

c# - 真实世界的开源 c# 应用程序展示了良好的代码

c# - 统一 DLL 的 Monotouch.Dialog 替代方案

windows - "Android Create"在 Windows 7 中调用失败 - 缺少 JDK

windows-10 - Windows 应用认证工具包挂起

windows - 在 Windows 10 周年纪念版中显示触摸键盘 (TabTip.exe)

c# - 如何用SQL Table填充DataTable

c# - 针对 Active Directory 进行身份验证的 Web 服务的安全密码解决方案?

c - .C 到 R 的接口(interface)

c++ - 在鼠标下滚动窗口

c# - 向 MediaComposition 添加背景颜色