请问各位老大,我现在想在程序中删除一个有文件的文件夹,应该用什么函数?原来用的是shell,不过麻烦不少,api中我也没找到这种函数,请问还有别的好方法吗?

解决方案 »

  1.   

    DeleteFolder(str.GetBuffer(str.GetLength()+2));
      

  2.   

    RemoveDirectoryThe RemoveDirectory function deletes an existing empty directory.
    BOOL RemoveDirectory(
      LPCTSTR lpPathName
    );Parameters
    lpPathName 
    [in] Pointer to a null-terminated string that specifies the path of the directory to be removed. The path must specify an empty directory, and the calling process must have delete access to the directory. 
    In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.
    Windows Me/98/95:  This string must not exceed MAX_PATH characters.
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError.Res
    To recursively delete the files in a directory, use the SHFileOperation function.Windows Me/98/95:  RemoveDirectoryW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
      

  3.   

    删除文件夹需要用到递归,不过你也可以利用Shell API取巧://////////////////////////////////////////////////////////////////////////
    // DelTree
    // 删除一个文件夹
    // lpszPath - 要删除的文件夹路径
    // 返回值:成功返回TRUE,否则返回FALSE
    // 备注:亦可用来删除单个文件
    //////////////////////////////////////////////////////////////////////////
    BOOL DelTree( LPCTSTR lpszPath )
    {
        SHFILEOPSTRUCT FileOp;
        FileOp.fFlags = FOF_NOCONFIRMATION;
        FileOp.hNameMappings = NULL;
        FileOp.hwnd = NULL;
        FileOp.lpszProgressTitle = NULL;
        FileOp.pFrom = lpszPath;
        FileOp.pTo = NULL;
        FileOp.wFunc = FO_DELETE;
        return SHFileOperation( &FileOp ) == 0;
    }
      

  4.   

    RemoveDirectory 这个函数可以删除有文件的文件夹么?
      

  5.   

    我在msdn上查了一下DeleteFolder,他说是这个是一个IMAPIFolder,请问这东西我能直接用么?
      

  6.   

    删除目录,包含删除子文件夹以及其中的内容BOOL DeleteDirectory(char *DirName)//如删除 DeleteDirectory("c:\\aaa")
    {
            CFileFind tempFind;
            char tempFileFind[MAX_PATH];
            sprintf(tempFileFind,"%s\\*.*",DirName);
            BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
            while(IsFinded)
            {
                    IsFinded=(BOOL)tempFind.FindNextFile();
                    if(!tempFind.IsDots())
                    {
                            char foundFileName[MAX_PATH];
                            strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
                            if(tempFind.IsDirectory())
                            {
                                    char tempDir[MAX_PATH];
                                    sprintf(tempDir,"%s\\%s",DirName,foundFileName);
                                    DeleteDirectory(tempDir);
                            }
                            else
                            {
                                    char tempFileName[MAX_PATH];
                                    sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
                                    DeleteFile(tempFileName);
                            }
                    }
            }
            tempFind.Close();
            if(!RemoveDirectory(DirName))
            {
                    MessageBox(0,"删除目录失败!","警告信息",MB_OK);//比如没有找到文件夹,删除失败,可把此句删除
                    return FALSE;
            }
            return TRUE;
    }