How to check if directory exist using C++ and winAPI [duplicate]

删除回忆录丶 提交于 2019-11-27 11:06:44

问题


Possible Duplicate:
How do you check if a directory exists on Windows in C?

How do I check whether a directory exists using C++ and windows API?


回答1:


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



回答2:


If linking to the shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function




回答3:


This code might work:

//if the directory exists
 DWORD dwAttr = GetFileAttributes(str);
 if(dwAttr != 0xffffffff && (dwAttr & FILE_ATTRIBUTE_DIRECTORY)) 



回答4:


0.1 second Google search:

BOOL DirectoryExists(const char* dirName) {
  DWORD attribs = ::GetFileAttributesA(dirName);
  if (attribs == INVALID_FILE_ATTRIBUTES) {
    return false;
  }
  return (attribs & FILE_ATTRIBUTE_DIRECTORY);
}


来源:https://stackoverflow.com/questions/8233842/how-to-check-if-directory-exist-using-c-and-winapi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!