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 PathFileExists in 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?
如何使用Win32程序检查文件是否存在?
4 个解决方案
#1
79
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中。
#2
22
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 _access
with access
. For a real platform agnostic solution, use the Boost FileSystem library.
编辑:要在Linux中编译,请替换
#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;
}
#3
9
If linking to the Shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function.
如果链接到Shell轻量级API (shlwapi.dll),您可以使用PathIsDirectory函数。
#4
4
Another option is the shell function PathFileExists()
另一个选项是shell函数PathFileExists()
PathFileExists() documentation
PathFileExists()的文档
This function "Determines whether a path to a file system object such as a file or directory is valid."
此函数“确定文件或目录等文件系统对象的路径是否有效”。
#1
79
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中。
#2
22
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 _access
with access
. For a real platform agnostic solution, use the Boost FileSystem library.
编辑:要在Linux中编译,请替换
#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;
}
#3
9
If linking to the Shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function.
如果链接到Shell轻量级API (shlwapi.dll),您可以使用PathIsDirectory函数。
#4
4
Another option is the shell function PathFileExists()
另一个选项是shell函数PathFileExists()
PathFileExists() documentation
PathFileExists()的文档
This function "Determines whether a path to a file system object such as a file or directory is valid."
此函数“确定文件或目录等文件系统对象的路径是否有效”。