比如,有文件夹d:\mydir\tt\,在tt中有一些文件,如何删除?好象没有现成的函数,分
98和XP两种情况,在线等,谢谢

解决方案 »

  1.   

    先将文件夹内的文件一一删除,再将文件夹删除。
    remove()删文件,
    rmdir()删目录。
      

  2.   

    下面的代码用于删除文件夹ttSHFILEOPSTRUCT sfo;
    memset(&sfo, 0, sizeof(sfo));
    sfo.fFlags = FOF_NOCONFIRMATION;
    sfo.wFunc = FO_DELETE;
    sfo.pFrom = "d:\\mydir\\tt";
    ::SHFileOperation(&sfo);
      

  3.   

    删除不为空的文件夹是一个递归过程,下面这个函数可以删除不为空的文件夹
    BOOL DeleteDirectory(CString sDirectory)
    {
        CString sPath = sDirectory;
        sPath += "\\*.*";    CFileFind finder;
        CString sFileName;
        BOOL bIsFindFile = finder.FindFile(sPath);
        while (bIsFindFile)
        {
            bIsFindFile = finder.FindNextFile();
            if (!finder.IsDots())
            {
                sFileName = finder.GetFilePath();
                if (finder.IsDirectory())
                {
                    ::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
                    if (!DeleteDirectory(sFileName))
                    {
                        return FALSE;
                    }
                }
                else
                {
                    ::SetFileAttributes(sFileName, FILE_ATTRIBUTE_ARCHIVE);
                    ::DeleteFile(sFileName);
                }
            }
        }
        finder.Close();
        ::SetFileAttributes(sDirectory, FILE_ATTRIBUTE_ARCHIVE);
        if (!::RemoveDirectory(sDirectory))
        {
            return FALSE;
        }
        return TRUE;
    }
      

  4.   


      Platform SDK: Storage 
    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.Requirements
    Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
    Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
    Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.
    See Also
    Directory Management Functions, CreateDirectoryPlatform SDK Release: February 2003  What did you think of this topic?
      Order a Platform SDK CD  Requirements
    Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
    Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
    Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.See Also
    Directory Management Functions, CreateDirectory
      

  5.   

    BOOL EmptyDirectory(LPCTSTR szPath, BOOL bDeleteDesktopIni, 
     BOOL bWipeIndexDat)
    {
    WIN32_FIND_DATA wfd;
    HANDLE hFind;
    CString sFullPath;
    CString sFindFilter;
    DWORD dwAttributes = 0; sFindFilter = szPath;
    sFindFilter += _T("\\*.*");
    if ((hFind = FindFirstFile(sFindFilter, &wfd)) == INVALID_HANDLE_VALUE)
    {
    return FALSE;
    } do
    {
    if (_tcscmp(wfd.cFileName, _T(".")) == 0 || 
    _tcscmp(wfd.cFileName, _T("..")) == 0 ||
    (bDeleteDesktopIni == FALSE && _tcsicmp(wfd.cFileName, _T("desktop.ini")) == 0))
    {
    continue;
    } sFullPath = szPath;
    sFullPath += _T('\\');
    sFullPath += wfd.cFileName; //去掉只读属性
    dwAttributes = GetFileAttributes(sFullPath);
    if (dwAttributes & FILE_ATTRIBUTE_READONLY)
    {
    dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
    SetFileAttributes(sFullPath, dwAttributes);
    } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    EmptyDirectory(sFullPath, bDeleteDesktopIni, bWipeIndexDat);
    RemoveDirectory(sFullPath);
    }
    else
    {
    if (bWipeIndexDat && _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
    {
    WipeFile(szPath, wfd.cFileName);
    } DeleteFile(sFullPath);
    }
    }
    while (FindNextFile(hFind, &wfd));
    FindClose(hFind); return TRUE;
    }
      

  6.   

    BOOL DelDirectory(CString DirName)
    {
        CFileFind tempFind;
        char tempFileFind[200];
        sprintf(tempFileFind,"%s\\*.*",DirName);
        BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
        while(IsFinded)
    {
          IsFinded=(BOOL)tempFind.FindNextFile();
          if(!tempFind.IsDots())
          {
                char foundFileName[200];
                strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
                if(tempFind.IsDirectory())
                {
                      char tempDir[200];
                      sprintf(tempDir,"%s\\%s",DirName,foundFileName);
                      DelDirectory(tempDir);
                }
                else
                {
                      char tempFileName[200];
                      sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
                      DeleteFile(tempFileName);
                }
            }
      }
      tempFind.Close();
      if(!RemoveDirectory(DirName))
      {
          ::MessageBox(0,"删除目录失败!","警告信息",MB_OK);
          return FALSE;
      }
      return TRUE;
    }