如何获取(逻辑)桌面上的项目总数 (C#)

发布于 2024-12-04 16:35:06 字数 828 浏览 1 评论 0原文

让我详细说明一下。我所说的“项目”是指您在桌面(Windows)上看到的所有项目,包括“我的电脑”、“回收站”、所有快捷方式等。如果我选​​择桌面上的所有项目,我会在属性中获得计数显示。我想要的就是这个计数,以编程方式。

我面临的问题:

我们看到的桌面包含我帐户中的项目,还有所有用户的桌面项目以及其他快捷方式,例如“我的电脑”、“回收站”。总共有3件事。所以我不能只从桌面目录的物理路径获取项目计数。所以这失败了:

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

我知道 SpecialFolder.Desktop 代表我们所看到的逻辑桌面。但是,由于 GetFolderPath() 再次获取用户桌面的物理路径,这再次失败:

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

获取用户桌面上的总数的正确方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離殇 2024-12-11 16:35:06

Windows shell 对此有充分且全面的支持。

  1. 调用 SHGetDesktopFolder() 获取 < a href="http://msdn.microsoft.com/en-us/library/bb775075.aspx" rel="nofollow">IShellFolder对于桌面。
  2. 调用 IShellFolder::EnumObjects() 来获取内容。

这篇代码项目文章从 C# 角度提供了一些使用示例。

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.

抱着落日 2024-12-11 16:35:06

这是不可能以您想要的方式实现的。

您可能忘记了任何桌面上都有一些元素,这些元素与文件无关(文件或链接),而是基于注册表,您肯定会错过它们。

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.

仅一夜美梦 2024-12-11 16:35:06

我正在为自己回答我在此处发布的提示和链接的帮助下终于找到的答案。

    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;
    }

与此同时,我学到了一件有趣的(相当烦人的!)事情。您在屏幕上看到的桌面与桌面的文件夹视图不同。即使您取消选中“我的电脑”和“我的文档”不在桌面上(您在显示器上看到的桌面),这些图标仍然可以出现在桌面的文件夹视图中。我尝试了此链接中给出的解决方案,但它给出了存在的项目数在文件夹视图中。我上面发布的解决方案将产生我想要的完美结果。该解决方案来自 此处,作者:叶志新。感谢@C.Evenhuis 的提示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文