naw*_*fal 5 c# windows desktop special-folders c#-2.0
让我详细说明一下."items"我指的是你在桌面上看到的所有项目(Windows),其中包括"我的电脑","回收站",所有快捷方式等.如果我选择桌面上的所有项目,我会在属性中获得计数显示.这是我想要的,以编程方式.
我面临的问题:
我们看到的桌面包含我帐户中All Users的项目,还有桌面项目以及"我的电脑","回收站"等其他快捷方式.总共有3件事.所以我不能只从物理路径到Desktop目录获取项目数.所以这失败了:
int count =
Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
.DesktopDirectory)
).Length;
Run Code Online (Sandbox Code Playgroud)
据我所知SpecialFolder.Desktop,我知道代表逻辑桌面.但是再次失败,因为GetFolderPath()再次获得用户桌面的物理路径:
int count =
Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder
.Desktop)
).Length;
Run Code Online (Sandbox Code Playgroud)
在用户的桌面上获得总计数的正确方法是什么?
naw*_*fal -3
我正在为自己回答我在此处发布的提示和链接的帮助下终于找到的答案。
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;
}
Run Code Online (Sandbox Code Playgroud)
与此同时,我学到了一件有趣的(相当烦人的!)事情。您在屏幕上看到的桌面与桌面的文件夹视图不同。即使您取消选中“我的电脑”和“我的文档”不在桌面上(您在显示器上看到的桌面),这些图标仍然可以出现在桌面的文件夹视图中。我尝试了此链接中给出的解决方案,但它给出了文件夹视图中存在的项目数。我上面发布的解决方案将产生我想要的完美结果。解决方案是从这里得到的,叶志新。感谢@C.Evenhuis 的提示。
| 归档时间: |
|
| 查看次数: |
1056 次 |
| 最近记录: |