且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 C++ 和 winAPI 检查目录是否存在

更新时间:2022-04-01 23:51:23

这是一个简单的函数,它就是这样做的:

Here is a simple function which does exactly this :

#include <windows.h>
#include <string>

bool dirExists(const std::string& dirName_in)
{
  DWORD ftyp = GetFileAttributesA(dirName_in.c_str());
  if (ftyp == INVALID_FILE_ATTRIBUTES)
    return false;  //something is wrong with your path!

  if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
    return true;   // this is a directory!

  return false;    // this is not a directory!
}