簡體   English   中英

如何在Windows中使用C ++刪除目錄

[英]How to remove directories using c++ in windows

我正在嘗試使用此代碼刪除目錄,但這不起作用。 而且,我找不到問題。 也許在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。

有什么好主意嗎?

Windows API已經帶有刪除整個目錄的功能。 那就是SHFileOperation 在Vista或更高版本上,出於相同目的使用IFileOperation

這不僅使該過程變得微不足道,而且只需要一個襯板,它還有其他好處:

  1. 您可以選擇將刪除的目錄放入回收站。
  2. 您可以選擇顯示標准系統進度對話框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM