'C#, Process Viewer, which is service vs desktop app

I'm not writing a process viewer, but need somewhat the same concept. I can do

List<Process> allProcesses = Process.GetProcesses().ToList();

Question: How can I tell which process is a normal process and which is a windows service? I've dug through the properties of Process and not finding a flag of any sort.



Solution 1:[1]

private bool IsService(Process p, out string serviceName)
{
    bool retVal = false;
    serviceName = "";

    string query = $"SELECT Name FROM Win32_Service WHERE ProcessId={p.Id}";
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject obj in searcher.Get())
    {
        serviceName = obj["Name"].ToString();
        retVal = true;
        break;
    }

    return retVal;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chizl