如何获取(逻辑)桌面上的项目总数 (C#)
让我详细说明一下。我所说的“项目”是指您在桌面(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Windows shell 对此有充分且全面的支持。
SHGetDesktopFolder()
获取 < a href="http://msdn.microsoft.com/en-us/library/bb775075.aspx" rel="nofollow">IShellFolder
对于桌面。IShellFolder::EnumObjects()
来获取内容。这篇代码项目文章从 C# 角度提供了一些使用示例。
The Windows shell has full and comprehensive support for this.
SHGetDesktopFolder()
to get anIShellFolder
for the desktop.IShellFolder::EnumObjects()
to get the contents.This Code Project article gives some usage examples from a C# perspective.
这是不可能以您想要的方式实现的。
您可能忘记了任何桌面上都有一些元素,这些元素与文件无关(文件或链接),而是基于注册表,您肯定会错过它们。
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.
我正在为自己回答我在此处发布的提示和链接的帮助下终于找到的答案。
与此同时,我学到了一件有趣的(相当烦人的!)事情。您在屏幕上看到的桌面与桌面的文件夹视图不同。即使您取消选中“我的电脑”和“我的文档”不在桌面上(您在显示器上看到的桌面),这些图标仍然可以出现在桌面的文件夹视图中。我尝试了此链接中给出的解决方案,但它给出了存在的项目数在文件夹视图中。我上面发布的解决方案将产生我想要的完美结果。该解决方案来自 此处,作者:叶志新。感谢@C.Evenhuis 的提示。
I'm answering for myself the answer I finally found out with the help of tips and links posted here.
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.