1

是否有与MFC中的 Java File 方法isDirectory()等效的方法?我试过用这个:


static bool isDirectory(CString &path) {
  return GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY;   
}

但它似乎不起作用。

4

4 回答 4

4

CFileFind::IsDirectory()

http://msdn.microsoft.com/en-us/library/scx99850(VS.80).aspx

编辑:

  #include <afxwin.h>
  #include <iostream>

  using namespace std;

  CFileFind finder;

  fileName += _T("c:\\aDirName");
  if (finder.FindFile(fileName))
  {
        if (finder.FindNextFIle())
        {            
              if (finder.IsDirectory())
              {
                    // Do directory stuff...
              }
        }
  }

如果您将文件名更改为具有通配符,您可以执行

  while(finder.findNextFile()) {...

获取所有匹配的文件。

于 2008-12-08T20:43:01.443 回答
2

抱歉,问题的答案可能“不一致”,但您可能会发现它很有用,因为每当我在 Windows 中需要这样的东西时,我不使用 MFC,而是使用常规的 Windows API:

//not completely tested but after some debug I'm sure it'll work
bool IsDirectory(LPCTSTR sDirName)
{
    //First define special structure defined in windows
    WIN32_FIND_DATA findFileData; ZeroMemory(&findFileData, sizeof(WIN32_FIND_DATA));
    //after that call WinAPI function finding file\directory
    //(don't forget to close handle after all!)
    HANDLE hf = ::FindFirstFile(sDirName, &findFileData);
    if (hf  ==  INVALID_HANDLE_VALUE) //also predefined value - 0xFFFFFFFF
    return false;
    //closing handle!
    ::FindClose(hf);
    // true if directory flag in on
    return (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
于 2008-12-08T22:35:48.440 回答
1

请求的 MFC 解决方案:a_FSItem 路径 ot 要测试的项目(检查 CFile::GetStatus() 以获得所需的要求)。

  CFileStatus t_aFSItemStat;
  CFile::GetStatus( a_FSItem, t_aFSItemStat );

  if ( ( t_aFSItemStat.m_attribute & CFile::directory )
    return true;

  return false;

如果您希望将卷根包含为有效目录,只需将其添加到测试中

t_aFSItemStat.m_attribute & CFile::volume
于 2009-06-30T03:59:26.683 回答
1

它不是 MFC,但我使用这个:

bool IsValidFolder(LPCTSTR pszPath)
{
    const DWORD dwAttr = ::GetFileAttributes(pszPath);
    if(dwAttr != 0xFFFFFFFF)
    {
        if((FILE_ATTRIBUTE_DIRECTORY & dwAttr) &&
           0 != _tcscmp(_T("."), pszPath) &&
           0 != _tcscmp(_T(".."), pszPath))
        {
            return true;
        }
    }

    return false;
}
于 2009-07-03T03:12:42.443 回答