How do you check if a directory exists in Windows in C?

Question

In a Windows C application, I want to check the parameter passed to the function to make sure that the specified path exists. *

How do you check if a directory exists in Windows in C?

* I understand that you can get into the conditions of the race when you check the existence and time, when you use the path that it no longer exists, but I can deal with it.

Additional background

Knowing explicitly that a directory exists or does not exist can be difficult when access permissions are allowed. It is possible that when you try to determine if a directory exists, the process does not have access rights to the directory or parent directory. This is normal for my needs. If the directory does not exist OR I cannot access it, then both applications are considered as an invalid path failure in my application, so I do not need to distinguish. (Virtual) if your solution provides for this difference.

Any solution in C, C runtime library or Win32 API is fine, but ideally I would like to stick to commonly loaded libraries (like kernel32, user32, etc.) and avoid solutions that are standard libraries (like PathFileExists in Shlwapi .dll). Again, (virtual) bonus points if your solution is cross-platform.

Similar

How to check if a file exists or not using Win32?

+52
c windows winapi
Jun 02 '11 at 17:57
source share
4 answers

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 the Kernel32.dll file.

+76
Jun 02 2018-11-18T00:
source share

Here's a fully platform agnostic solution (using the standard C library)

Edit: To do this, on Linux, replace <io.h> with <unistd.h> and _access with access . For a real agnostic platform solution, use the Boost FileSystem library.

 #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 implementation that supports both MBCS and UNICODE assemblies:

 #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; } 
+20
Jun 02 2018-11-18T00:
source share

If the Shell Lightweight API link (shlwapi.dll) is right for you, you can use the PathIsDirectory function .

+7
Mar 23 '13 at 7:37
source share

Another option is the shell function PathFileExists ()

Documentation PathFileExists ()

This function "Determines whether the path to a file system object such as a file or directory is valid."

+3
Aug 28 '14 at 3:10
source share



All Articles