代码之家  ›  专栏  ›  技术社区  ›  Slapout

用于删除文件夹的win32 api函数是什么?

  •  21
  • Slapout  · 技术社区  · 17 年前

    用于程序删除文件和文件夹的win32 API是什么?

    编辑

    DeleteFile RemoveDirectory 是我在找的。 但是,对于这个项目,我最终使用了 SHFileOperation . 我找到了 sample code 在codeguru帮助。

    6 回复  |  直到 9 年前
        1
  •  15
  •   itsmatt    17 年前
        2
  •  28
  •   hatcat    17 年前

    有两种方法可以解决这个问题。一个是通过 File Services (使用诸如 DeleteFile RemoveDirectory )另一个是通过 Windows Shell (使用) SHFileOperation )如果要删除非空目录或需要资源管理器样式的反馈(例如,带有飞行文件的进度对话框),建议使用后者。最快的方法是创建一个 SHFILEOPSTRUCT ,初始化并调用 紧急手术 因此:

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

    这会自动删除整个目录。您可以通过改变shfileopstruct初始化来添加反馈和提示-请仔细阅读。

        3
  •  12
  •   Mark Lakata    14 年前

    请参见上面的uvgroovy评论。“dir”字段末尾需要2个空值。

    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,
        "",
        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.
    }
    
        4
  •  8
  •   Ajay    9 年前

    我相信 DeleteFile 不将文件发送到回收站。也, RemoveDirectory 只删除空目录。 SHFileOperation 如果需要的话,您可以最大限度地控制要删除的内容和方法,并显示标准的Windows UI对话框(例如“准备删除等”)。

        5
  •  2
  •   Prasaathviki    13 年前
        /* 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);
                }
    
        6
  •  1
  •   aquirdturtle    10 年前

    对于C++编程,如果您愿意使用第三方库, boost::filesystem::remove_all(yourPath) 比shfileoperation简单得多。