我怎么能知道一个目录真的是一个回收站? [英] How can I tell that a directory is really a recycle bin?

查看:290
本文介绍了我怎么能知道一个目录真的是一个回收站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,给定一个路径,告诉我它是一个回收站文件夹。我尝试使用类似SHGetSpecialFolderPath与CSIDL_BITBUCKET功能,但这不工作,因为回收站是一个虚拟文件夹,是所有驱动器的回收站的联合。

I need a function that, given a path, tells me whether it is a Recycle Bin folder. I tried using functions like SHGetSpecialFolderPath with CSIDL_BITBUCKET, but that doesn't work because the Recycle Bin is a virtual folder that is the union of the Recycle Bins of all drives.


这个问题是要记录在 http://blogs.msdn.com/oldnewthing/archive/2008/09/18/8956382.aspx

推荐答案

http://blogs.msdn.com/oldnewthing/archive/2008/09/18/8956382.aspx


客户注意到他们不是
想要硬编码字词
RECYCLED RECYCLER ,这是一个很好的决定,因为名称的
目录依赖于很多东西。
它...取决于
文件系统。它还取决于
驱动器是否本地访问
或远程;基于网络的回收站
文件夹遵循另一个命名
方案。它甚至可能取决于用户正在运行什么
操作系统。
不,硬编码的
回收站文件夹的名称不是一个好的
的想法。

The customer noted that they don't want to hard-code the words RECYCLED and RECYCLER, which is a good decision because the name of the directory depends on many things. It ... depends on the file system. It also depends on whether the drive is accessed locally or remotely; network-based Recycle Bin folders follow yet another naming scheme. It may even depend on what operating system the user is running. No, hard-coding the name of the Recycle Bin folders is not a good idea.

SHDESCRIPTIONID结构告诉
你多一点关于shell
文件夹。除了
description ID,它还给你一个
CLSID,这里是
相关的CLSID。

The SHDESCRIPTIONID structure tells you a little more about a shell folder. In addition to the "description ID", it also gives you a CLSID, and it is the CLSID that is relevant here.

#include <windows.h>
#include <shlobj.h>
#include <tchar.h>
#include <stdio.h>

HRESULT GetFolderDescriptionId(LPCWSTR pszPath, SHDESCRIPTIONID *pdid)
{
  HRESULT hr;
  LPITEMIDLIST pidl;
  if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL,&pidl, 0, NULL))) {
    IShellFolder *psf;
    LPCITEMIDLIST pidlChild;
    if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder, (void**)&psf, &pidlChild))) {
      hr = SHGetDataFromIDList(psf, pidlChild,SHGDFIL_DESCRIPTIONID, pdid, sizeof(*pdid));
      psf->Release();
    }
    CoTaskMemFree(pidl);
  }
  return hr;
}

int __cdecl wmain(int argc, WCHAR **argv)
{
  SHDESCRIPTIONID did;
  if (SUCCEEDED(GetFolderDescriptionId(argv[1], &did)) && did.clsid == CLSID_RecycleBin) {
    printf("is a recycle bin\n");
  } else {
    printf("is not a recycle bin\n");
  }
  return 0;
}

GetFolderDescriptionId函数
获取文件夹的路径,
将它转换为ITEMIDLIST只是这样
它可以调用SHGetDataFromIDList到
获得SHDESCRIPTIONID。在这种情况下,我们所有的
关心是否
CLSID是回收站。

The GetFolderDescriptionId function takes the path to a folder and converts it to an ITEMIDLIST just so it can call SHGetDataFromIDList to obtain the SHDESCRIPTIONID. All we care about in this case is whether the CLSID is the Recycle Bin or not.

C:\> checkrecycle C:\Windows
is not a recycle bin
C:\> checkrecycle C:\RECYCLER\S-1-5-21-2127521184-1604012920-1887927527-72713
is a recycle bin


这篇关于我怎么能知道一个目录真的是一个回收站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆