简体   繁体   中英

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

Programmatically, how can I determine the 3 categories in Windows 10

  • Apps
  • Background process
  • Windows service

Just like Task Manager?

ie I need some C# code which I can determine a list of Apps vs. a list of background process. Checking executionState in Win32_process doesn't work. It's always null.

Thanks!

在此处输入图片说明

The original problem:

  • How to detect list of running apps?

I have a different take regarding the solution:

Some UWP apps has the main window title. Checking main window title is not enough to say the app is running or not.

UWP apps in suspend state will still return the title (see the red rectangle)

在此处输入图片说明

So in order to detect the suspend state, we need to cover

  1. app has title but not running in the foreground
  2. app has title but not running in the background

{code}

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();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM