Microsoft KB Archive/105306: Difference between revisions

From BetaArchive Wiki
m (1 revision imported: importing part 2)
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 28: Line 28:
<pre class="CODESAMP">  HANDLE hFile;
<pre class="CODESAMP">  HANDLE hFile;


   hFile = CreateFile( &quot;c:\\mstools&quot;,
   hFile = CreateFile( "c:\\mstools",
               GENERIC_READ | GENERIC_WRITE,
               GENERIC_READ | GENERIC_WRITE,
               FILE_SHARE_READ | FILE_SHARE_WRITE,
               FILE_SHARE_READ | FILE_SHARE_WRITE,
Line 37: Line 37:
           );
           );
   if( hFile == INVALID_HANDLE_VALUE )
   if( hFile == INVALID_HANDLE_VALUE )
       MessageBox( NULL, &quot;CreateFile() failed&quot;, NULL, MB_OK ); </pre>
       MessageBox( NULL, "CreateFile() failed", NULL, MB_OK ); </pre>
The handle obtained can be used to obtain information about the directory or to set information about the directory. For example:
The handle obtained can be used to obtain information about the directory or to set information about the directory. For example:


Line 45: Line 45:
   char msg[40];
   char msg[40];


   GetFileInformationByHandle( hFile, &amp;fiBuf );
   GetFileInformationByHandle( hFile, &fiBuf );
   FileTimeToLocalFileTime( &amp;fiBuf.ftLastWriteTime, &amp;ftBuf );
   FileTimeToLocalFileTime( &fiBuf.ftLastWriteTime, &ftBuf );
   FileTimeToSystemTime( &amp;ftBuf, &amp;stBuf );
   FileTimeToSystemTime( &ftBuf, &stBuf );
   wsprintf( msg, &quot;Last write time is %d:%d  %d/%d/%d&quot;,
   wsprintf( msg, "Last write time is %d:%d  %d/%d/%d",
       stBuf.wHour,stBuf.wMinute,stBuf.wMonth,stBuf.wDay,stBuf.wYear );
       stBuf.wHour,stBuf.wMinute,stBuf.wMonth,stBuf.wDay,stBuf.wYear );
   MessageBox( NULL, msg, NULL, MB_OK ); </pre>
   MessageBox( NULL, msg, NULL, MB_OK ); </pre>

Latest revision as of 12:25, 21 July 2020

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.