如何使用 C++ 和 winAPI 检查目录是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8233842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if directory exist using C++ and winAPI
提问by MaSmi
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?
如何使用 C++ 和 Windows API 检查目录是否存在?
回答by FailedDev
well we were all n0obsat some point in time. No problem in asking. Here is a simple function which does exactly this :
好吧,我们在某个时间点都是n0obs。问没有问题。这是一个简单的函数,它正是这样做的:
#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!
}
回答by Simon Mourier
If linking to the shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function
如果链接到外壳轻量级 API (shlwapi.dll) 对您来说没问题,您可以使用PathIsDirectory 函数
回答by CopiedFromGoogle
This code might work:
此代码可能有效:
//if the directory exists
DWORD dwAttr = GetFileAttributes(str);
if(dwAttr != 0xffffffff && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
回答by CopiedFromGoogle
0.1 second Google search:
0.1秒谷歌搜索:
BOOL DirectoryExists(const char* dirName) {
DWORD attribs = ::GetFileAttributesA(dirName);
if (attribs == INVALID_FILE_ATTRIBUTES) {
return false;
}
return (attribs & FILE_ATTRIBUTE_DIRECTORY);
}