C语言 你如何在 C 中检查 Windows 上是否存在目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6218325/
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 do you check if a directory exists on Windows in C?
提问by Zach Burlingame
Question
题
In a Windows C application I want to validate a parameter passed into a function to ensure that the specified path exists.*
在 Windows C 应用程序中,我想验证传递给函数的参数以确保指定的路径存在。*
How do you check if a directory exists on Windows in C?
你如何在 C 中检查 Windows 上是否存在目录?
*I understand that you can get into race conditions where between the time you check for the existance and the time you use the path that it no longer exists, but I can deal with that.
*我知道您可能会遇到竞争条件,在您检查存在的时间和您使用它不再存在的路径的时间之间,但我可以处理。
Additional Background
附加背景
Knowing explicitly that a directory does or does not exist can get tricky when permissions come into play. It's possible that in attempting to determine if the directory exists, the process doesn't have permissions to access the directory or a parent directory. This is OK for my needs. If the directory doesn't exist OR I can't access it, both are treated as an invalid path failure in my application, so I don't need to differentiate. (Virtual) bonus points if your solution provides for this distinction.
当权限发挥作用时,明确知道目录存在或不存在可能会变得棘手。有可能在尝试确定目录是否存在时,进程没有访问目录或父目录的权限。这可以满足我的需要。如果目录不存在或我无法访问它,则两者都在我的应用程序中被视为无效路径故障,因此我不需要区分。如果您的解决方案提供了这种区别,则(虚拟)奖励积分。
Any solution in the C language, C runtime library, or Win32 API is fine, but ideally I'd like to stick to libraries that are commonly loaded (e.g. kernel32, user32, etc.) and avoid solutions that involve loading non-standard libraries (like PathFileExistsin Shlwapi.dll). Again, (Virtual) bonus points if your solution is cross-platform.
C 语言、C 运行时库或 Win32 API 中的任何解决方案都很好,但理想情况下我想坚持使用通常加载的库(例如 kernel32、user32 等)并避免涉及加载非标准库的解决方案(如Shlwapi.dll 中的PathFileExists)。同样,如果您的解决方案是跨平台的,则(虚拟)奖励积分。
Related
有关的
How can we check if a file Exists or not using Win32 program?
回答by retrodrone
Do something like this:
做这样的事情:
BOOL DirectoryExists(LPCTSTR szPath)
{
DWORD dwAttrib = GetFileAttributes(szPath);
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
The GetFileAttributes() method is included in Kernel32.dll.
的GetFileAttributes()方法被包括在Kernel32.dll中。
回答by dario_ramos
Here's a totally platform-agnostic solution (using the standard C library)
这是一个完全与平台无关的解决方案(使用标准 C 库)
Edit:For this to compile in Linux, replace <io.h>with <unistd.h>and _accesswith access. For a real platform agnostic solution, use the Boost FileSystem library.
编辑:对于这个在Linux中编译,替换<io.h>用<unistd.h>和_access用access。对于真正的平台无关解决方案,请使用 Boost FileSystem 库。
#include <io.h> // For access().
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
bool DirectoryExists( const char* absolutePath ){
if( _access( absolutePath, 0 ) == 0 ){
struct stat status;
stat( absolutePath, &status );
return (status.st_mode & S_IFDIR) != 0;
}
return false;
}
A Windows-specific implementation that supports both MBCS and UNICODE builds:
支持 MBCS 和 UNICODE 构建的特定于 Windows 的实现:
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <tchar.h>
BOOL directory_exists( LPCTSTR absolutePath )
{
if( _taccess_s( absolutePath, 0 ) == 0 )
{
struct _stat status;
_tstat( absolutePath, &status );
return (status.st_mode & S_IFDIR) != 0;
}
return FALSE;
}
回答by Simon Mourier
If linking to the Shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function.
如果链接到 Shell Lightweight API (shlwapi.dll) 对您来说没问题,您可以使用PathIsDirectory 函数。
回答by Greg
Another option is the shell function PathFileExists()
另一种选择是外壳函数 PathFileExists()
PathFileExists() documentation
This function "Determines whether a path to a file system object such as a file or directory is valid."
此函数“确定文件系统对象(例如文件或目录)的路径是否有效。”

