【问题标题】:How to remove directories using c++ in windows如何在windows中使用c++删除目录
【发布时间】:2014-03-24 19:49:43
【问题描述】:

我正在尝试使用此代码删除目录,但这不起作用。而且,我找不到问题。可能是 Windows 上的删除操作缺少权限?

这是我正在使用的代码:

#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;
}

操作系统: Windows 7 SP1。

请问有什么绝妙的主意吗?

【问题讨论】:

  • 我不确定,但我认为在迭代文件和目录时删除它们不是一个好主意。收集名称,然后在完成迭代后删除。
  • 那么当你运行代码时会发生什么?
  • @codah,什么都没有,目录还在,cmd等待击键。
  • 我想你可能会收到一些操作系统错误/消息或其他东西
  • 我猜你知道你遇到了系统错误 32,这是一个共享冲突。我想也许这是was your issue?

标签: c++


【解决方案1】:

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

这不仅使过程变得微不足道,只不过是一个班轮,而且还有其他好处:

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

【讨论】:

  • 我没有找到。你能写一个删除一个文件夹的例子吗?
  • 这样做非常简单。您只需拨打SHFileOperation。像这样声明SHFILEOPSTRUCT 结构:SHFILEOPSTRUCT shfo = { 0 }; 并填充其字段。您需要将wFunc 作为FO_DELETE,将pFrom 作为文件夹名称,以双空结尾,然后将fFlags 设置为您想要的任何内容。我相信你可以写出那几行代码。
  • 因为你可以给我们提供样品很简单,不是吗?
  • 对不起,我不明白那个评论。请详细说明一下。
猜你喜欢
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多