3

参照: codeguru.com/forum/showthread.php?t=239271

以下の機能を使用してフォルダを削除すると、最上位のフォルダを除いて、すべてのフォルダ、サブフォルダ、およびファイルが削除されます。を除いて、その下にあるすべてのものは削除されc:\folder1\folder2ます。folder2folder2

BOOL DeleteDirectory(const TCHAR* sPath)  
{  
    HANDLE hFind; // file handle
    WIN32_FIND_DATA FindFileData;

    TCHAR DirPath[MAX_PATH];
    TCHAR FileName[MAX_PATH];

    _tcscpy(DirPath,sPath);
    _tcscat(DirPath,_T("\\"));
    _tcscpy(FileName,sPath);
    _tcscat(FileName,_T("\\*")); // searching all files
    int nRet = 0;
    hFind = FindFirstFile(FileName, &FindFileData); // find the first file
    if( hFind != INVALID_HANDLE_VALUE ) 
    {
        do
        {
            if( IsDots(FindFileData.cFileName) ) 
                continue; //if not directory continue

            _tcscpy(FileName + _tcslen(DirPath), FindFileData.cFileName);
            if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 
            {
                // we have found a directory, recurse
                if( !DeleteDirectory(FileName) ) 
                    break;   // directory couldn't be deleted
            }
            else 
            {
                if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
                    _wchmod(FileName, _S_IWRITE); // change read-only file mode

                if( !DeleteFile(FileName) ) 
                    break;  // file couldn't be deleted
            }
        }while( FindNextFile(hFind, &FindFileData) );

        nRet = FindClose(hFind); // closing file handle
    }

    return RemoveDirectory(sPath); // remove the empty (maybe not) directory and returns zero when RemoveDirectory function fails
}  

問題を見つけるための助けをいただければ幸いです。FindCloseデバッグ中に、関数がファイルハンドルを正常に閉じていたが、32 を返していることに気付きましたGetLastError(「別のプロセスで使用されているため、プロセスはファイルにアクセスできません」)。

4

3 に答える 3

6

SHFileOperationこの方法でディレクトリを削除できますが、 passingを呼び出すことで、システムに削除を任せる方が簡単ですFO_DELETE。このAPIに渡す文字列をdoublenullで終了する必要があることに注意してください。

于 2012-03-13T12:41:59.977 に答える
2

再帰呼び出しの前にファイル ハンドルを閉じる必要があると思います。つまり、再帰呼び出しを終了した後、ファイル ハンドルを適切なものに再度設定する必要があります。

SHFileOperation はより良い解決策かもしれません。なぜ彼らのコードが意図したとおりに機能しなかったのかというOPの質問に答えているだけです。

于 2012-08-28T17:36:17.920 に答える
1

参照:http://www.codeguru.com/forum/archive/index.php/t-337897.html
以下は、SHFileOperation を使用してディレクトリを削除するコードです。

bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true)
{
    int len = _tcslen(lpszDir);
    TCHAR* pszFrom = new TCHAR[len+4]; //4 to handle wide char
    //_tcscpy(pszFrom, lpszDir); //todo:remove warning//;//convet wchar to char*
    wcscpy_s (pszFrom, len+2, lpszDir);
    pszFrom[len] = 0;
    pszFrom[len+1] = 0;

    SHFILEOPSTRUCT fileop;
    fileop.hwnd   = NULL;    // no status display
    fileop.wFunc  = FO_DELETE;  // delete operation
    fileop.pFrom  = pszFrom;  // source file name as double null terminated string
    fileop.pTo    = NULL;    // no destination needed
    fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user

    if(!noRecycleBin)
        fileop.fFlags |= FOF_ALLOWUNDO;

    fileop.fAnyOperationsAborted = FALSE;
    fileop.lpszProgressTitle     = NULL;
    fileop.hNameMappings         = NULL;

    int ret = SHFileOperation(&fileop); //SHFileOperation returns zero if successful; otherwise nonzero 
    delete [] pszFrom;  
    return (0 == ret);
}
于 2012-03-13T13:25:20.127 に答える