小弟在写一个程序,删除目录下的文件,用的是下面Deltree函数,该函数是将目录下所有文件删除,但如果有文件正在使用,比如删除IE临时文件目录下的文件时,就会弹出“在存取 一未命名文件时发生共享违例”……    现在想改进函数,使得具有“判断文件是否在使用”的功能,如果正在使用则跳过该文件,删除剩余文件,还请高手帮忙看看啊!先谢谢了……void Deltree(CString Path)
{
    if(Path[Path.GetLength()-1]!='\\')
    Path+="\\";
    Path+="*.*";
    CFileFind finder;
    BOOL fstill=finder.FindFile(Path);
    while(fstill)
   {
       fstill=finder.FindNextFile();
       CString FilePath=finder.GetFilePath();
       if(finder.IsDirectory())
       {
          if(!finder.IsDots())
          {
              CFile file;
              CFileStatus fs;
              file.GetStatus(FilePath,fs);
              if(fs.m_attribute!=0x10)
              {
                  fs.m_attribute=0x10;
                  fs.m_mtime=0;
                  file.SetStatus(FilePath,fs);
               }
               ::SetFileAttributes(FilePath,FILE_ATTRIBUTE_NORMAL);
               if(!::RemoveDirectory(FilePath))
               {
                   Deltree(FilePath);
                   ::RemoveDirectory(FilePath);
                }
            }
         }
        else
       {
            CFile file;
            CFileStatus fs;
            file.GetStatus(FilePath,fs);
            fs.m_attribute=0;
            file.SetStatus(FilePath,fs);
            ::DeleteFile(FilePath);
        }
    }
}

解决方案 »

  1.   

    不用Deltree自己写//删除指定文件夹
    bool CYourClass::DeleteFolder(wchar_t* pwcPath )
    {
    if(pwcPath == NULL)
    return false; wchar_t wcPath[MAX_PATH] = {0};
    wcscpy_s(wcPath,MAX_PATH,pwcPath);
    wcscat_s(wcPath,MAX_PATH,_T("\\*.*"));
    WIN32_FIND_DATA FindFileData;
    ZeroMemory(&FindFileData,sizeof(WIN32_FIND_DATA)); HANDLE hFindFile = FindFirstFile(wcPath,&FindFileData); if(hFindFile == INVALID_HANDLE_VALUE)
    return false; BOOL bContinue = true; while (bContinue != false)
    {
    //bIsDots为真表示是.或..
    bool bIsDots = (wcscmp(FindFileData.cFileName,_T(".")) == 0 || wcscmp(FindFileData.cFileName,_T("..")) == 0);
    if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && bIsDots == false)
    {
    //是目录,就再进入该目录
    wcscpy_s(wcPath,MAX_PATH,pwcPath);
    wcscat_s(wcPath,MAX_PATH,_T("\\"));
    wcscat_s(wcPath,MAX_PATH,FindFileData.cFileName);
    DeleteFolder(wcPath);
    //寻找下一文件
    bContinue = FindNextFile(hFindFile,&FindFileData);
    continue;
    } if (bIsDots == false)
    {
    //是文件删除之
    wcscpy_s(wcPath,MAX_PATH,pwcPath);
    wcscat_s(wcPath,MAX_PATH,_T("\\"));
    wcscat_s(wcPath,MAX_PATH,FindFileData.cFileName);
    DeleteFile(wcPath);
    }
    //寻找下一文件
    bContinue = FindNextFile(hFindFile,&FindFileData); } FindClose(hFindFile); //删除空目录
    RemoveDirectory(pwcPath);
    return true;
    }
      

  2.   

    谢谢niuxiaojia09,不过说实话,小弟对wcscat_s函数不清楚,VC6.0下编译提示wcscpy_s : undeclared identifier等错误,其使用是不是要注意什么?
      

  3.   

    wcscat_s是VC++ 2005里字符串安全操作的函数,VC6.0用wcscat就可以了。
      

  4.   

    if(Path[Path.GetLength()-1]!='\\')
      Path+="\\";
    这种写法有问题,如果Path.GetLength()==0,那么Path[-1]就有错误了...
      

  5.   

    如果你是VC6的话,把所有wcscpy_s和wcscat_s都去掉_s
    然后去掉MAX_PATH这个参数,
    例如:
    wcscpy(wcPath,pwcPath);