问题
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