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

前端 未结 6 2086
情书的邮戳
情书的邮戳 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:45
        /* function used to send files and folder to recycle bin in win32 */
                int fn_Send_Item_To_RecycleBin(TCHAR newpath[]) 
                {          
                _tcscat_s(newpath, MAX_PATH,_T("|"));
                TCHAR* Lastptr = _tcsrchr(newpath, _T('|'));
                *Lastptr = _T('\0');                                         // Replace last pointer with Null for double null termination
                SHFILEOPSTRUCT shFileStruct; 
                ZeroMemory(&shFileStruct,sizeof(shFileStruct)); 
                shFileStruct.hwnd=NULL; 
                shFileStruct.wFunc= FO_DELETE; 
                shFileStruct.pFrom= newpath;
                shFileStruct.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
                return SHFileOperation(&shFileStruct);
                }
    
    0 讨论(0)
  • 2020-12-29 22:50

    I think you want DeleteFile and RemoveDirectory

    0 讨论(0)
  • 2020-12-29 22:52

    For C++ programming, if you're willing to work with third-party libraries, boost::filesystem::remove_all(yourPath) is much simpler than SHFileOperation.

    0 讨论(0)
  • 2020-12-29 22:56

    See uvgroovy's comment above. You need 2 nulls at the end of the 'dir' field.

    int silently_remove_directory(LPCTSTR dir) // Fully qualified name of the directory being   deleted,   without trailing backslash
    {
      int len = strlen(dir) + 2; // required to set 2 nulls at end of argument to SHFileOperation.
      char* tempdir = (char*) malloc(len);
      memset(tempdir,0,len);
      strcpy(tempdir,dir);
    
      SHFILEOPSTRUCT file_op = {
        NULL,
        FO_DELETE,
        tempdir,
        NULL,
        FOF_NOCONFIRMATION |
        FOF_NOERRORUI |
        FOF_SILENT,
        false,
        0,
        "" };
      int ret = SHFileOperation(&file_op);
      free(tempdir);
      return ret; // returns 0 on success, non zero on failure.
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-29 23:00

    I believe DeleteFile does not send the file to the Recycle Bin. Also, RemoveDirectory removes only empty dirs. SHFileOperation would give you the most control over what and how to delete and would show the standard Windows UI dialog boxes (e.g. "Preparing to delete etc.) if needed.

    0 讨论(0)
提交回复
热议问题