What is the Win32 API function to use to delete a folder?

前端 未结 6 2177
情书的邮戳
情书的邮戳 2020-12-29 22:10

What are the Win32 APIs to use to programically delete files and folders?

Edit

DeleteFile and RemoveDirectory are what I was looking for.

6条回答
  •  无人及你
    2020-12-29 22:57

    There are two ways to approach this. One is through the File Services (using commands such as DeleteFile and RemoveDirectory) and the other is through the Windows Shell (using SHFileOperation). The latter is recommended if you want to delete non-empty directories or if you want explorer style feedback (progress dialogs with flying files, for example). The quickest way of doing this is to create a SHFILEOPSTRUCT, initialise it and call SHFileOperation, thus:

    void silently_remove_directory(LPCTSTR dir) // Fully qualified name of the directory being deleted, without trailing backslash
    {
        SHFILEOPSTRUCT file_op = {
            NULL,
            FO_DELETE,
            dir,
            "",
            FOF_NOCONFIRMATION |
            FOF_NOERRORUI |
            FOF_SILENT,
            false,
            0,
            "" };
        SHFileOperation(&file_op);
    }
    

    This silently deletes the entire directory. You can add feedback and prompts by varying the SHFILEOPSTRUCT initialisation - do read up on it.

提交回复
热议问题