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

前端 未结 6 2087
情书的邮戳
情书的邮戳 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: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.
    }
    

提交回复
热议问题