删除目录功能访问被拒绝

问题描述 投票:0回答:1

我正在使用下面的C ++代码从特定路径删除临时文件。

C:\ Users \ falcon \ AppData \ Local \ Temp \〜GPG.TMP \

当〜GPG.TMP文件夹中存在某些文件时,它将首先删除,最后删除文件夹本身。在此最后一步RemoveDirectory(sPathName)中,我没有任何异常,但实际上并未删除该文件夹,但是当我尝试(从外部或通过编程方式)访问该文件夹时,我收到错误13,即“权限被拒绝”。为什么会这样?

void CFileOperation::DoDelete(CString sPathName)
{
    CFileFind ff;
    CString sPath = sPathName;

    if (CheckPath(sPath) == PATH_IS_FILE)
    {
        if (!CanDelete(sPath)) 
        {
            m_bAborted = true;
            return;
        }
        if (!DeleteFile(sPath)) throw new CFExeption(GetLastError());
        return;
    }

    PreparePath(sPath);
    sPath += "*.*";

    BOOL bRes = ff.FindFile(sPath);

    while(bRes)
    {
        bRes = ff.FindNextFile();
        if (ff.IsDots()) continue;
        if (ff.IsDirectory())
        {

            sPath = ff.GetFilePath();
            DoDelete(sPath);
        }
        else DoDelete(ff.GetFilePath());
    }
    ff.Close();

    if (!RemoveDirectory(sPathName) && !m_bAborted) {

        throw new CFExeption(GetLastError());


    }

}
c++ file visual-c++ mfc permission-denied
1个回答
0
投票

根据MSDN:RemoveDirectory功能在关闭时将目录标记为删除。因此,在关闭目录的最后一个句柄之前,不会删除目录。

而且,恕我直言,错误13是ERROR_INVALID_DATA

© www.soinside.com 2019 - 2024. All rights reserved.