Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
File Services
File Systems
File Management
 SetFileInformationByHandle Function
SetFileInformationByHandle Function

Sets the file information for the specified file.

To retrieve file information using a file handle, see GetFileInformationByHandle or GetFileInformationByHandleEx.

Syntax

C++
BOOL WINAPI SetFileInformationByHandle(
  __in  HANDLE hFile,
  __in  FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
  __in  LPVOID lpFileInformation,
  __in  DWORD dwBufferSize
);

Parameters

hFile [in]

A handle to the file for which to change information.

This handle must be opened with the appropriate permissions for the requested change. For more information, see the Remarks and Example Code sections.

This handle should not be a pipe handle.

FileInformationClass [in]

A FILE_INFO_BY_HANDLE_CLASS enumeration value that specifies the type of information to be changed.

For a table of valid values, see the Remarks section.

lpFileInformation [in]

A pointer to the buffer that contains the information to change for the specified file information class. The structure that this parameter points to corresponds to the class that is specified by FileInformationClass.

For a table of valid structure types, see the Remarks section.

dwBufferSize [in]

The size of lpFileInformation, in bytes.

Return Value

Returns nonzero if successful or zero otherwise.

To get extended error information, call GetLastError.

Remarks

Certain file information classes behave slightly differently on different operating system releases. These classes are supported by the underlying drivers, and any information they return is subject to change between operating system releases.

The following table shows the valid file information classes and their corresponding data structure types for use with this function.

FileInformationClass valuelpFileInformation type
FileBasicInfoFILE_BASIC_INFO
FileRenameInfoFILE_RENAME_INFO
FileDispositionInfoFILE_DISPOSITION_INFO
FileAllocationInfoFILE_ALLOCATION_INFO
FileEndOfFileInfoFILE_END_OF_FILE_INFO
FileIoPriorityHintInfoFILE_IO_PRIORITY_HINT_INFO

 

You must specify appropriate access flags when creating the file handle for use with SetFileInformationByHandle. For example, if the application is using FILE_DISPOSITION_INFO with the DeleteFile member set to true, the file would need DELETE access requested in the call to the CreateFile function. To see an example of this, see the Example Code section. For more information about file permissions, see File Security and Access Rights.

If there is a transaction bound to the handle, then the changes made will be transacted for the information classes FileBasicInfo, FileRenameInfo, FileAllocationInfo, FileEndOfFileInfo, and FileDispositionInfo. If FileDispositionInfo is specified, only the delete operation is transacted if a DeleteFile operation was requested. In this case, if the transaction is not committed before the handle is closed, the deletion will not occur. For more information about TxF, see Transactional NTFS (TxF).

Examples

The following C++ example shows how to create a file and mark it for deletion when the handle is closed.

//...
   HANDLE hFile = CreateFile (TEXT("tempfile"), 
                              GENERIC_READ | GENERIC_WRITE | DELETE,
                              0 /* exclusive access */,
                              NULL, CREATE_ALWAYS,
                              0, NULL);

   if (hFile != INVALID_HANDLE_VALUE)
   {
      FILE_DISPOSITION_INFO fdi;
      fdi.DeleteFile = TRUE; // marking for deletion

      BOOL fResult = SetFileInformationByHandle(
                                hFile, 
                                FileDispositionInfo, 
                                &fdi, 
                                sizeof(FILE_DISPOSITION_INFO)
                                );
      
      if (fResult)
      {
         // File will be deleted upon CloseHandle.
         _tprintf(
             TEXT("SetFileInformationByHandle marked tempfile for deletion\n"));
         
         // ... 
         // Now use the file for whatever temp data storage you need,
         // it will automatically be deleted upon CloseHandle or 
         // application termination.
         // ...
      }
      else
      {
         _tprintf(
             TEXT("error %lu:  SetFileInformationByHandle could not mark tempfile for deletion\n"), 
             GetLastError());
      }
      
      CloseHandle(hFile); 
      
      // At this point, the file is closed and deleted by the system.
   }
   else 
   {
      _tprintf(
          TEXT("error %lu:  could not create tempfile\n"), 
          GetLastError());
   }
//...

Requirements

Minimum supported clientWindows Vista
Minimum supported serverWindows Server 2008
RedistributableWindows SDK on Windows Server 2003 and Windows XP.
HeaderWinBase.h (include Windows.h), FileExtd.h on Windows Server 2003 and Windows XP
LibraryKernel32.lib, FileExtd.lib on Windows Server 2003 and Windows XP
DLLKernel32.dll

See Also

CreateFile
File Management Functions
File Security and Access Rights
Generic Access Rights
GetFileInformationByHandle
GetFileInformationByHandleEx

Send comments about this topic to Microsoft

Build date: 10/22/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
When using FILE_RENAME_INFO...      Jon Schwartz - MSFT   |   Edit   |   Show History
...note that dwBufferSize is the combined size of the structure and the file name (embedded in the structure).
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker