关于C#:删除目录功能访问被拒绝

Remove Directory function access denied

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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());


    }

}


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

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