在本地NTFS驱动器上查找回收站

时间:2009-06-01 19:40:52

标签: windows winapi filesystems ntfs recycle-bin

我正在尝试编写一些简单的代码,它将返回本地驱动器上的回收站目录。看起来很简单 - 应该是谷歌上的一千个答案。还没有找到一个:(

我发现FAT和NTFS驱动器有不同的基本名称(RECYCLED和RECYCLER)。我发现''回收站是一个虚拟文件夹,它结合了机器上所有驱动器的回收箱。

我还没有找到一种方法来查找C:drive的回收站目录 - 即使是在越南语(或任何其他非英语)计算机上。 (我找不到任何帖子表明“RECYCLER”是否国际化)

有人能指出我一个明确的答案吗?

由于

更新:意识到CSIDL_BITBUCKET以及使用它的功能。从我读过的所有内容中,它指向虚拟目录,该目录是该用户在所有驱动器上删除的所有文件的并集。寻找物理回收站目录(在我的Vista上,据我所知它似乎是C:\ $ Recycle.Bin)

4 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

使用Raymond Chen的建议和其他人的技术(不记得我在哪里找到它)我提出了一个函数,可以在驱动器上找到Recycle Bin目录。该函数循环查看根目录中的目录,查看隐藏和/或系统目录。当它找到一个时,它会检查子子目录,寻找具有CLSID_Recycle Bin的子目录。

请注意,我在下面包含了两个GetFolderCLSID函数。 Raymond Chen是更简单的一种,但它在Windows 2000上不起作用。另一种实现方式更长,但似乎无处不在。

调用如:CString recycleDir = FindRecycleBinOnDrive(L“C:\”);

CString FindRecycleBinOnDrive(LPCWSTR path)
{
    CString search;
    search.Format(L"%c:\\*", path[0]);
    WIN32_FIND_DATA fd = {0};
    HANDLE fHandle = FindFirstFile(search, &fd);
    while(INVALID_HANDLE_VALUE != fHandle)
    {
        if(FILE_ATTRIBUTE_DIRECTORY == (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) //only check directories
        {
            if(0 != (fd.dwFileAttributes & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) //only check hidden and/or system directories
            {
                //the recycle bin directory itself won't be marked, but a SID-specific child directory will, so now look at them
                CString childSearch;
                childSearch.Format(L"%c:\\%s\\*", path[0], fd.cFileName);
                WIN32_FIND_DATA childFD = {0};
                HANDLE childHandle = FindFirstFile(childSearch, &childFD);
                while(INVALID_HANDLE_VALUE != childHandle)
                {
                    if((FILE_ATTRIBUTE_DIRECTORY == (childFD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) && //only check directories
                        (childFD.cFileName[0] != L'.')) //don't check . and .. dirs
                    {
                        CString fullPath;
                        fullPath.Format(L"%c:\\%s\\%s", path[0], fd.cFileName, childFD.cFileName);
                        CLSID id = {0};
                        HRESULT hr = GetFolderCLSID(fullPath, id);
                        if(SUCCEEDED(hr))
                        {
                            if(IsEqualGUID(CLSID_RecycleBin, id))
                            {
                                FindClose(childHandle);
                                FindClose(fHandle);
                                //return the parent (recycle bin) directory
                                fullPath.Format(L"%c:\\%s", path[0], fd.cFileName);
                                return fullPath;
                            }
                        }
                        else
                        {
                            Log(logERROR, L"GetFolderCLSID returned %08X for %s", hr, fullPath);
                        }
                    }

                    if(FALSE == FindNextFile(childHandle, &childFD))
                    {
                        FindClose(childHandle);
                        childHandle = INVALID_HANDLE_VALUE;
                    }
                }
            }
        }
        if(FALSE == FindNextFile(fHandle, &fd))
        {
            FindClose(fHandle);
            fHandle = INVALID_HANDLE_VALUE;
        }
    }
    _ASSERT(0);
    return L"";
}


//Works on Windows 2000, and even as Local System account
HRESULT GetFolderCLSID(LPCWSTR path, CLSID& pathCLSID)
{
    LPMALLOC pMalloc = NULL;
    HRESULT hr = 0;
    if (SUCCEEDED(hr = SHGetMalloc(&pMalloc))) 
    {
        LPSHELLFOLDER pshfDesktop = NULL;
        if (SUCCEEDED(hr = SHGetDesktopFolder(&pshfDesktop))) 
        {
            LPITEMIDLIST pidl = NULL;
            DWORD dwAttributes = SFGAO_FOLDER;
            if (SUCCEEDED(hr = pshfDesktop->ParseDisplayName(NULL, NULL, (LPWSTR)path, NULL, &pidl, &dwAttributes))) 
            {
                LPPERSIST pPersist = NULL;
                if (SUCCEEDED(hr = pshfDesktop->BindToObject(pidl, NULL, IID_IPersist, (LPVOID *) &pPersist))) 
                {
                    hr = pPersist->GetClassID(&pathCLSID); 
                    pPersist->Release();
                } 
                pMalloc->Free(pidl);
            } 
            pshfDesktop->Release();
        } 
        pMalloc->Release();
    }
    return hr;
}


//Not supported on Windows 2000 since SHParseDisplayName wasn't implemented then
//HRESULT GetFolderCLSID(LPCWSTR pszPath, CLSID& pathCLSID)
//{
//  SHDESCRIPTIONID did = {0};
//  HRESULT hr = 0;
//  LPITEMIDLIST pidl = NULL;
//  if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL, &pidl, 0, NULL))) //not supported by Windows 2000
//  {
//      IShellFolder *psf = NULL;
//      LPCITEMIDLIST pidlChild = NULL;
//      if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder, (void**)&psf, &pidlChild))) 
//      {
//          hr = SHGetDataFromIDList(psf, pidlChild, SHGDFIL_DESCRIPTIONID, &did, sizeof(did));
//          psf->Release();
//          pathCLSID = did.clsid;
//      }
//      CoTaskMemFree(pidl);
//  }
//  return hr;
//}

答案 2 :(得分:1)

有点晚了,但也许迟到总比没好......

调试shell32.dll后,我发现对于每个版本的Windows,循环路径都是硬编码的,并且还取决于该驱动器的文件系统。我在Windows XP,Vista和Windows7上测试了这个:

让X:成为我们想要获取回收站路径的驱动器,让SID成为当前用户的SID,然后:


    switchif(OsType) {
        case WindowsXP:
        {
            if(PartitionType("X:") == NTFS)
            {
                printf("Path is: X:\\Recycler\\SID\\");
            }
            else
            {
                printf("Path is X:\\RECYCLED\\");
            }
        }

        case WindowsVista:
        case Windows7:
        {
            if(PartitionType("X:") == NTFS)
            {
                printf("Path is: X:\\$Recycle.bin\\SID\\");
            }
            else
            {
                printf("Path is X:\\$RECYCLE.BIN\\");
            }
        }
    }

维基文章提供了相同的事实: http://en.wikipedia.org/wiki/Recycle_Bin_%28Windows%29

答案 3 :(得分:0)

在Win32中,使用SHGetSpecialFolderLocation。将CSIDL_BITBUCKET作为CDIL参数传递。