如何判断目录是否有效?

解决方案 »

  1.   

    目录有效是什么意思?如果判断目录是否存在,可以用SetCurrentDirectory判断。
      

  2.   

    bool CFunctions::IsDirExits(CString strDir)
    {
    WIN32_FIND_DATA wfd; // 查找
    HANDLE hFind=FindFirstFile(strDir,&wfd); 
    if (hFind==INVALID_HANDLE_VALUE)
    return false; // 没有找到配备,目录肯定不存在
    else {
    if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // 检查找到的结果是否目录
    return true; // 是目录,目录存在
    else
    return false; // 是目录,目录不存在
    FindClose(hFind);

    }
      

  3.   

    用_chdir函数,改变目录到你所需要检查的目录下进行检测if (_chdir(dir) != 0)
    return false;
      

  4.   

    仅供参考
    void CListCtrlExtra::addFileFolder(LPCTSTR filename)
    {
    struct _stat buf;
    int result = _tstat(filename, &buf );
    if( result == 0 ) //if result !=0, then it failed altogether
    { // a directory?
    if ((buf.st_mode & _S_IFDIR) == _S_IFDIR)
    { WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    CString dirpath = filename;
    dirpath+="\\*"; hFind = FindFirstFile(dirpath, &FindFileData); if (hFind == INVALID_HANDLE_VALUE)
    { //printf ("Invalid File Handle. Get Last Error reports %d\n", GetLastError ());
    }
    else
    { //look into directory
    do
    { if(FindFileData.cFileName[0]!='.')
    { dirpath = filename;
    dirpath += "\\";
    dirpath += FindFileData.cFileName;
    //InsertItem(GetItemCount(), dirpath);
    addFileFolder(dirpath); //recursively add directories, or just add the file
    }
    }
    while(FindNextFile(hFind, &FindFileData));
    FindClose(hFind);
    } }
    else //file not folder
    { if(!allowduplicates)
    { for(int i=0;i<GetItemCount();i++)
    { if(GetItemText(i,0).CompareNoCase(filename)==0)
    { return; //match found, do not add the file again
    }
    }
    }
    InsertItem(GetItemCount(), filename);
    }
    }
    }
      

  5.   

    借用宝地一下 问一个问题  有一目录:d:\dir1\dir2  我怎样得到dir2
      

  6.   

    两个办法:
    1、
        CFileFind f;
        if(f.FindFile("c:\\temp"))
            AfxMessageBox("存在");
            //Directory exist.
        else
            AfxMessageBox("不存在");
            //Not exist.2、
        if(_access("c:\\temp", 0) == 0){
            AfxMessageBox("存在");
        }
        else{
            AfxMessageBox("不存在");
        }
      

  7.   

    還有一種方法判斷是否存在.
    CFileStatus rStatus;
    if(!CFile::Open(目錄名,rStatus)){
      //不存在.也可以用來判斷文件.
    }
    是否有效到底是甚麼意思?如果是判斷文件名是否正確的話.就是判斷文件名裡面不應該存在非法子符就可以了