驱动中牛
|
阅读:1911回复:0
共享:目录删除代码(C++)
BOOL
DeleteDirectoryEx( __in CONST wchar_t* pDir ) { if((pDir == NULL)) return false; INT len = static_cast<INT>(wcslen(pDir)); CONST wchar_t *Path=pDir; wchar_t Dir[MAX_PATH]; memset(Dir,'\0',MAX_PATH); wmemcpy_s(Dir,MAX_PATH,Path,wcslen(Path)); if(Path[len]!='\\') Dir[len]='\\'; wchar_t Files[MAX_PATH]; memset(Files,'\0',MAX_PATH); wmemcpy_s(Files,MAX_PATH,Dir,wcslen(Dir)); wcscat_s(Files,MAX_PATH,_T("*.*")); WIN32_FIND_DATA wfd; HANDLE hFind=FindFirstFile(Files,&wfd); BOOL Ret=true; wchar_t Tmp[MAX_PATH]; memset(Tmp,'\0',MAX_PATH); wmemcpy_s(Tmp,MAX_PATH,Dir,wcslen(Dir)); if (hFind!=INVALID_HANDLE_VALUE) { BOOL bFind=true; while (bFind) { //if(wfd.cFileName[0]!='.') // . .. if ((wcscmp(wfd.cFileName,_T(".")) != 0) && (wcscmp(wfd.cFileName,_T("..")) != 0)) { memset(Tmp,'\0',MAX_PATH); wmemcpy_s(Tmp,MAX_PATH,Dir,wcslen(Dir)); wcscat_s(Tmp,MAX_PATH,wfd.cFileName); if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { Ret = Ret && DeleteDirectoryEx(Tmp); } else { SetFileAttributes(Tmp,FILE_ATTRIBUTE_NORMAL); Ret = Ret && DeleteFile(Tmp); } } bFind = FindNextFile(hFind,&wfd); } FindClose(hFind); } return RemoveDirectory(Path); } |