2014-03-24 60 views
-1

我试图删除使用此代码的目录,但这不起作用。而且,我找不到这个问题。在Windows上删除操作可能缺乏权限?如何在Windows中使用C++删除目录

这里是我正在使用的代码:

#define _CRT_SECURE_NO_DEPRECATE 
#include <string> 
#include <iostream> 

#include <windows.h> 
#include <conio.h> 


int DeleteDirectory(const std::string &refcstrRootDirectory, 
    bool    bDeleteSubdirectories = true) 
{ 
    bool   bSubdirectory = false;  // Flag, indicating whether 
    // subdirectories have been found 
    HANDLE   hFile;      // Handle to directory 
    std::string  strFilePath;     // Filepath 
    std::string  strPattern;     // Pattern 
    WIN32_FIND_DATA FileInformation;    // File information 


    strPattern = refcstrRootDirectory + "\\*.*"; 
    hFile = ::FindFirstFile((LPCWSTR)strPattern.c_str(), &FileInformation); 
    if (hFile != INVALID_HANDLE_VALUE) 
    { 
     do 
     { 
      if (FileInformation.cFileName[0] != '.') 
      { 
       strFilePath.erase(); 
       strFilePath = refcstrRootDirectory + "\\" + (char*)FileInformation.cFileName; 

       if (FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
       { 
        if (bDeleteSubdirectories) 
        { 
         // Delete subdirectory 
         int iRC = DeleteDirectory(strFilePath, bDeleteSubdirectories); 
         if (iRC) 
          return iRC; 
        } 
        else 
         bSubdirectory = true; 
       } 
       else 
       { 
        // Set file attributes 
        if (::SetFileAttributes((LPCWSTR)strFilePath.c_str(), 
         FILE_ATTRIBUTE_NORMAL) == FALSE) 
         return ::GetLastError(); 

        // Delete file 
        if (::DeleteFile((LPCTSTR)strFilePath.c_str()) == FALSE) 
         return ::GetLastError(); 
       } 
      } 
     } while (::FindNextFile(hFile, &FileInformation) == TRUE); 

     // Close handle 
     ::FindClose(hFile); 

     DWORD dwError = ::GetLastError(); 
     if (dwError != ERROR_NO_MORE_FILES) 
      return dwError; 
     else 
     { 
      if (!bSubdirectory) 
      { 
       // Set directory attributes 
       if (::SetFileAttributes((LPCWSTR)refcstrRootDirectory.c_str(), 
        FILE_ATTRIBUTE_NORMAL) == FALSE) 
        return ::GetLastError(); 

       // Delete directory 
       if (::RemoveDirectory((LPCWSTR)refcstrRootDirectory.c_str()) == FALSE) 
        return ::GetLastError(); 
      } 
     } 
    } 

    return 0; 
} 


int main() 
{ 
    int   iRC = 0; 
    std::string strDirectoryToDelete = "C:\\Users\\AbysCo\\Desktop\\del"; 


    // Delete 'c:\mydir' without deleting the subdirectories 
    iRC = DeleteDirectory(strDirectoryToDelete, false); 
    if (iRC) 
    { 
     std::cout << "Error " << iRC << std::endl; 
     return -1; 
    } 

    // Delete 'c:\mydir' and its subdirectories 
    iRC = DeleteDirectory(strDirectoryToDelete); 
    if (iRC) 
    { 
     std::cout << "Error " << iRC << std::endl; 
     return -1; 
    } 

    // Wait for keystroke 
    _getch(); 

    return 0; 
} 

OS:的Windows 7 SP1。

有什么好主意吗?

+0

我不确定,但我不认为这是一个好主意,当它们迭代时删除文件和目录。收集名称,然后在完成迭代后删除。 –

+0

那么当你运行代码时会发生什么? – codah

+0

@codah,什么都没有,目录仍然存在cmd等待击键。 – Siro

回答

2

Windows API已经提供了删除整个目录的功能。那是SHFileOperation。在Vista或更高版本中,您可以使用IFileOperation来达到同样的目的。

这不仅使过程琐碎,并不比一个更衬,但它有其他好处:

  1. 您可以将已删除的目录放入回收站,如果你选择。
  2. 如果您选择,您可以显示标准系统进度对话框。
+0

你可以做个节目吗? – Siro

+0

这里有很多例子。你应该去一趟。 –

+0

我还未找到任何。你可以写一个删除一个文件夹的例子吗? – Siro