以编程方式,我如何确定 Windows 10 中的 3 个类别
- 应用
- 后台进程
- Windows 服务
就像任务管理器一样?
即我需要一些 C# 代码,我可以确定应用程序列表与后台进程列表。检查 Win32_process 中的 executionState 不起作用。它始终为空。
谢谢!
最佳答案
原问题:
- 如何检测正在运行的应用列表?
我对解决方案有不同的看法:
某些 UWP 应用程序具有主窗口标题。 检查主窗口标题不足以判断应用程序是否正在运行。
处于挂起状态的 UWP 应用程序仍将返回标题(见红色矩形)
所以为了检测挂起状态,我们需要覆盖
- 应用有标题但没有在前台运行
- 应用有标题但不在后台运行
{代码}
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/