Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the total number of items on the (logical) desktop (C#)

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?

like image 583
nawfal Avatar asked Nov 26 '22 11:11

nawfal


1 Answers

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.

like image 197
David Heffernan Avatar answered Dec 15 '22 22:12

David Heffernan