MFC 相当于 Java File#isDirectory()

发布于 2024-07-09 19:02:46 字数 235 浏览 4 评论 0原文

MFC 中是否有与 Java 文件方法 isDirectory() 等效的方法? 我尝试使用这个:


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

但它似乎不起作用。

Is there an equivalent to the Java File method isDirectory() in MFC? I tried using this :


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

but it doesn't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一花一树开 2024-07-16 19:02:46

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...
              }
        }
  }

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

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

来获取所有匹配的文件。

CFileFind::IsDirectory()

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

EDIT:

  #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...
              }
        }
  }

If you change filename to have wildcards, you can do a

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

to get all matching files.

看春风乍起 2024-07-16 19:02:46

很抱歉问题的答案可能“不一致”,但也许你会发现它很有用,因为每当我在 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;
}

Sorry for possibly "inconsistency" of answer to question but may be you'll see it useful because anytime I need something like this in Windows I am NOT using MFC but regular 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;
}
欢你一世 2024-07-16 19:02:46

根据要求MFC解决方案:
a_FSItem 要测试的项目的路径(检查 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

MFC solution as requested:
a_FSItem path ot the item to test (examine the CFile::GetStatus() for the needed requirements).

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

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

  return false;

if you wish to include a volume root as a valid directory just add it to the test

t_aFSItemStat.m_attribute & CFile::volume
墨落画卷 2024-07-16 19:02:46

它不是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;
}

Its not MFC, but I use this:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文