How to get Directory information via Windows native API?

kernel32, winapi, windows

Solution

I ran into something like this once you have to pass this flag to get a valid handle for a directory. From the MSDN documentation.

try this

HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, 
         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS, NULL); 

`FILE_FLAG_BACKUP_SEMANTICS` | You must set this flag to obtain a handle to a directory. A directory handle can be passed to some functions instead of a file handle. For more information, see the Remarks section.

Problem

I can get the created date, file size etc for a file using the following code: ``` // Error handling removed for brevity HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); LARGE_INTEGER fileSize; GetFileSizeEx(hFile, &fileSize); FILE_BASIC_INFO fileInfo); GetFileInformationByHandle(hFile, FileBasicInfo, fileInfo, sizeof(fileInfo)); ``` But when called against a directory, all values are set to zero - how do I get directory info? Thanks

Original source