且构网

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

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

更新时间:2021-10-03 00:39:14

我们在某个时间点都是 n0obs 。没问题。这是一个简单的功能:

well we were all n0obs at some point in time. No problem in asking. 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!
}