HOWTO: Get and Use a Handle to a DirectoryLast reviewed: November 26, 1997Article ID: Q105306 |
The information in this article applies to:
SUMMARYCreateDirectory() 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 INFORMATIONOpening 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. Keywords : BseFileio Version : winnt:3.1,3.5,3.51,4.0; Platform : winnt Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |