Znalezienie wyłącznie otwartych programów w procesach Windowsa

0

problem procesy.PNGCześć
Czy mógłby mi ktoś pomóc? Chciałbym wyświetlić nazwy wszystkich otwartych programów w terminalu.
Na dopiero włączonym komputerze wszystko jest ok, ale po pewnym czasie zaczynają pojawiać się programy UWP (takie jak pogoda, newsy itd.) mimo, że ich nie otwierałem. System chyba sam je ładuje w tle, jak ma trochę luzu.

PS uwagi co do jakości kodu są mile widziane :)
Pozdrawiam

https://pastebin.com/5twis5TH
https://pastebin.com/m5uzjmsc

0
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Text;

/// <summary>
/// EnumDesktopWindows Demo - shows the caption of all desktop windows.
/// Authors: Svetlin Nakov, Martin Kulov 
/// Bulgarian Association of Software Developers - http://www.devbg.org/en/
/// </summary>
public class user32
{
    /// <summary>
    /// filter function
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    /// <summary>
    /// check if windows visible
    /// </summary>
    /// <param name="hWnd"></param>
    /// <returns></returns>
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

    /// <summary>
    /// return windows text
    /// </summary>
    /// <param name="hWnd"></param>
    /// <param name="lpWindowText"></param>
    /// <param name="nMaxCount"></param>
    /// <returns></returns>
    [DllImport("user32.dll", EntryPoint = "GetWindowText",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    /// <summary>
    /// enumarator on all desktop windows
    /// </summary>
    /// <param name="hDesktop"></param>
    /// <param name="lpEnumCallbackFunction"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

    /// <summary>
    /// entry point of the program
    /// </summary>
    static void Main()
    {
        var collection = new List<string>();
        user32.EnumDelegate filter = delegate (IntPtr hWnd, int lParam)
        {
            StringBuilder strbTitle = new StringBuilder(255);
            int nLength = user32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
            string strTitle = strbTitle.ToString();

            if (user32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
            {
                collection.Add(strTitle);
            }
            return true;
        };

        if (user32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
        {
            foreach (var item in collection)
            {
                Console.WriteLine(item);
            }
        }
        Console.Read();
    }
}

Źródło -> http://pinvoke.net/default.aspx/user32.EnumDesktopWindows

0

Dzięki za odp., ale twój kod także pokazuje programy UWP po pewnym czasie :( Niektóre nawet podwójnie. Poza tym kod jest podobny do mojego. Ja zamiast EnumDesktopWindows() użyłem EnumWindows() i dodałem proc.ProcessName != "ApplicationFrameHost" && proc.ProcessName != "ShellExperienceHost", żeby właśnie nie wyświetlały się podwójnie.

cmd output:

c:\users\patryk\documents\visual studio 2017\Projects\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe
ConsoleApp1 (Running) - Microsoft Visual Studio
Znalezienie wyłącznie otwartych programów w procesach Windowsa :: 4programmers.net - Google Chrome
Task Manager
Settings
News
Calculator
Store
Store
Weather
Weather
3D Builder
3D Builder
Calendar
Calendar
Mail
Inbox - Gmail ?- Mail
Settings
Photos
Photos
News
Calculator
Windows Shell Experience Host
Program Manager

1 użytkowników online, w tym zalogowanych: 0, gości: 1