5

Let me elaborate. By "items" I mean all the items you see one the desktop (Windows) which includes "My Computer", "Recycle Bin", all the shortcuts etc. If I select all the items on the desktop I get the count in the properties displayed. It is this count I want, programmatically.

The problem I face:

The desktop as we see has items from my account, also the All Users's desktop items and also other shortcuts like "My Computer", "Recycle Bin". In total, 3 things. So I can't just get the item count from the physical path to Desktop directory. So this fails:

int count =
    Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
                                                            .DesktopDirectory)
                      ).Length;

I know SpecialFolder.Desktop stands for the logical desktop as we see. But this fails again since GetFolderPath() again gets the physical path of user's desktop:

int count = 
    Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
                                                            .Desktop)
                      ).Length;

What is the right way to get total count on the user's desktop?

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
nawfal
  • 70,104
  • 56
  • 326
  • 368

3 Answers3

4

The Windows shell has full and comprehensive support for this.

  1. Call SHGetDesktopFolder() to get an IShellFolder for the desktop.
  2. Call IShellFolder::EnumObjects() to get the contents.

This Code Project article gives some usage examples from a C# perspective.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
-2

This is just not possible in a way you want it.

You probably forgot that there are elements on any desktop, which is not file-related (files, or links) but rather a registry-based, and you will miss them definetely.

Alan Turing
  • 2,482
  • 17
  • 20
-3

I'm answering for myself the answer I finally found out with the help of tips and links posted here.

    private const uint GET_ITEM_COUNT = 0x1000 + 4;



    [DllImport("user32.DLL")]

    private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

    [DllImport("user32.DLL")]

    private static extern IntPtr FindWindow(string lpszClass, string lpszWindow);

    [DllImport("user32.DLL")]

    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, 
                                                            string lpszClass, string lpszWindow);



    public static int GetDesktopCount()
    {
        //Get the handle of the desktop listview

        IntPtr vHandle = FindWindow("Progman", "Program Manager");

        vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);

        vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", "FolderView");


        //Get total count of the icons on the desktop

        int vItemCount = SendMessage(vHandle, GET_ITEM_COUNT, 0, 0);

        return vItemCount;
    }

There's an interesting (rather annoying!) thing I came to learn meanwhile. The desktop you see on your screen is different from the desktop's folder view. Even if you uncheck My Computer and MyDocument from being on desktop (the desktop you see on monitor), these icons can be still there in the folder view of the desktop. I tried the solution given in this link, but it gives the count of items present in the folder view. The solution I posted above would yield the perfect result I want. The solution was got from here, by Zhi-Xin Ye. Thanks @C.Evenhuis for the tip.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • You are relying on undocumented behavior here, by groveling around inside Explorer's window hierarchy and assuming that the icons on the desktop are presented by a listview. – Raymond Chen Sep 15 '11 at 13:03
  • @Raymond, but this method saved my ass. I couldn't find the output with other solutions. May be my lack of knowledge, I anyway couldn't tweak the code snippet you recommended to get an output of my desire. – nawfal Sep 15 '11 at 13:12
  • This is a simply terrible approach that will likely break on a future release of Windows. – David Heffernan Sep 15 '11 at 13:22
  • @David Heffernan, I don't have enough expertise on the Shell and APIs, so nothing solved the problem for me. I couldn't find a working a solution online as well. If you can provide, I'll appreciate. – nawfal Sep 15 '11 at 13:27
  • 2
    I believe Microsoft provides a C# code pack for shell programming in managed code. I don't have a link to it, but you can try searching for it. Please do not ship this code to customers - it may very well stop working in future versions of Windows, and then your customers will be stuck. – Raymond Chen Sep 15 '11 at 14:54
  • @Raymond yes I get that. Thanks – nawfal Sep 15 '11 at 15:21