Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c# - Getting a list of all applications

I'm trying to get the list of all the open applications. Specifically, if you open the task manager and go to the applications tab, that list.

I've tried using something like this:

foreach (var p in Process.GetProcesses())
{
    try
    {
        if (!String.IsNullOrEmpty(p.MainWindowTitle))
        {
            sb.Append("
");
            sb.Append("Window title: " + p.MainWindowTitle.ToString());
            sb.Append("
");
        }
    }
    catch
    {
    }
}

Like in a few examples I've found, but this doesn't pull all the applications for me. It's only grabbing about half the ones I can see in the task manager or that I know I have open. For example, this method doesn't pick up Notepad++ or Skype for some reason, but DOES pick up Google Chrome, Calculator, and Microsoft Word.

Does anyone know either why this isn't working correctly or how to do so?

Also, a friend suggested it might be a permissions issue, but I am running visual studio as administrator and it hasn't changed.

EDIT: The problem I'm getting is that most of the solutions I've been given just returns a list of ALL processes, which isn't what I want. I just want the open applications or windows, like the list that appears on the task manager. Not a list of every single process.

Also, I know there is bad code in here, including the empty catch block. This was a throwaway project just to figure out how this works in the first place.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code example here appears to give what you're asking for. Modified version:

public class DesktopWindow
{
    public IntPtr Handle { get; set; }
    public string Title { get; set; }
    public bool IsVisible { get; set; }
}

public class User32Helper
{
    public delegate bool EnumDelegate(IntPtr hWnd, int lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "GetWindowText",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

    [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
        ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction,
        IntPtr lParam);

    public static List<DesktopWindow> GetDesktopWindows()
    {
        var collection = new List<DesktopWindow>();
        EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
        {
            var result = new StringBuilder(255);
            GetWindowText(hWnd, result, result.Capacity + 1);
            string title = result.ToString();

            var isVisible = !string.IsNullOrEmpty(title) && IsWindowVisible(hWnd);

            collection.Add(new DesktopWindow { Handle = hWnd, Title = title, IsVisible = isVisible });

            return true;
        };

        EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
        return collection;
    }
}

With the above code, calling User32Helper.GetDesktopWindows() should give you a list of containing the Handle/Title for all open applications as well as whether or not they're visible. Note that true is returned regardless of the window's visibility, as the item would still show up in the Applications list of Task Manager as the author was asking.

You could then use the corresponding Handle property from one of the items in the collection to do a number of other tasks using other Window Functions (such as ShowWindow or EndTask).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...