HOWTO: Get and Use a Handle to a Directory
ID: Q105306
|
The information in this article applies to:
-
Microsoft Win32 Application Programming Interface (API), used with:
-
Microsoft Windows NT versions 3.1, 3.5, 3.51, 4.0
-
Microsoft Windows 2000
SUMMARY
CreateDirectory() can be used to open a new directory. An existing
directory can be opened by calling CreateFile(). To open an existing
directory with CreateFile(), it is necessary to specify the flag
FILE_FLAG_BACKUP_SEMANTICS. The following code shows how this can be done:
HANDLE hFile;
hFile = CreateFile( "c:\\mstools",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL
);
if( hFile == INVALID_HANDLE_VALUE )
MessageBox( NULL, "CreateFile() failed", NULL, MB_OK );
The handle obtained can be used to obtain information about the directory
or to set information about the directory. For example:
BY_HANDLE_FILE_INFORMATION fiBuf;
FILETIME ftBuf;
SYSTEMTIME stBuf;
char msg[40];
GetFileInformationByHandle( hFile, &fiBuf );
FileTimeToLocalFileTime( &fiBuf.ftLastWriteTime, &ftBuf );
FileTimeToSystemTime( &ftBuf, &stBuf );
wsprintf( msg, "Last write time is %d:%d %d/%d/%d",
stBuf.wHour,stBuf.wMinute,stBuf.wMonth,stBuf.wDay,stBuf.wYear );
MessageBox( NULL, msg, NULL, MB_OK );
MORE INFORMATION
Opening directories with CreateFile is not supported on Windows 95.
This code does not work on Win32s, because MS-DOS does not support opening
a directory. If you are looking for the creation time of a directory, use
FindFirstFile(), because it works on all platforms.
Additional query words:
Keywords : kbAPI kbFileIO kbKernBase kbWinOS2000 kbDSupport kbGrpKernBase
Version : winnt:3.1,3.5,3.51,4.0
Platform : winnt
Issue type : kbhowto