Microsoft KB Archive/105306

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

HOWTO: Get and Use a Handle to a Directory

Q105306



The information in this article applies to:


  • Microsoft Win32 Application Programming Interface (API), used with:
    • the operating system: Microsoft Windows NT, versions 3.1, 3.5, 3.51, 4.0
    • the operating system: 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 kbOSWin2000 kbDSupport kbGrpDSKernBase
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API


Last Reviewed: October 27, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.