SetFileInformationByHandle function (Windows)

Switch View :
ScriptFree
SetFileInformationByHandle function

Applies to: desktop apps | Metro style apps

Sets the file information for the specified file.

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

Syntax

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

FileBasicInfo

0

FILE_BASIC_INFO

FileRenameInfo

3

FILE_RENAME_INFO

FileDispositionInfo

4

FILE_DISPOSITION_INFO

FileAllocationInfo

5

FILE_ALLOCATION_INFO

FileEndOfFileInfo

6

FILE_END_OF_FILE_INFO

FileIoPriorityHintInfo

12

FILE_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.

C++

//...
  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 client

Windows Vista

Minimum supported server

Windows Server 2008

Redistributable

Windows SDK on Windows Server 2003 and Windows XP.

Header

FileAPI.h (include Windows.h);
WinBase.h on Windows Server 2008 R2, Windows 7, Windows Server 2008, and Windows Vista (include Windows.h);
FileExtd.h on Windows Server 2003 and Windows XP

Library

Kernel32.lib;
FileExtd.lib on Windows Server 2003 and Windows XP

DLL

Kernel32.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: 4/17/2012

Community Content

David Lowndes
Wondering what use FILE_DISPOSITION_INFO is? I think it's for performance!

If you've created a large file (you may have sized it soon after creating it using SetFilePointer(Ex) & SetEndOfFile) on a slow device (such as a USB flash memory device), when you close the handle, the file (all of it) is written to the device.

If sometime after creating/sizing the file you subsequently need to delete it rather than keep it, the normal method of closing the file handle and using DeleteFile can be incredibly slow - but setting the DeleteFile flag with this function makes the close operation much faster - and of course you don't need to use DeleteFile either.


ArielLaub
using this in conjunction with FILE_FLAG_NO_BUFFERING in windows XP
I'm trying to write to an unbuffered file handle but I'm having difficulties setting the EOF to a non sector aligned position.
I was able to do this using the function above but I would like to write this code so it will compile and link in both XP and Vista/7.

I also tried using the NtSetInformationFile (with no success) which looks almost identical but I would prefer not to use undocumented APIs..

Is there any other way to set the EOF? (SetEndOfFile only works on sector aligned positions...)



Thomas Lee
Anyone used SetFileInformationByHandle in practice for renaming of the files ,so

Anyone used SetFileInformationByHandle in practice for renaming of the files ,so
that she worked - i.e. renamed the files? With what parameter to open handle file? Beside me this function DELETES the files , but does NOT RENAME them. Can be this function does not work? Possible bring detailed working example of the use SetFileInformationByHandle for renaming of the file? It Beforehand is much thanked.


STRDISK="j" ! disk name
str251="AB"
STR251=ADJUSTL(STR251)
string0="\??\"//STRDISK//":" !//TRIM(STR251)//"\"
filedir="\??\"//STRDISK//":\" !//TRIM(STR251)//"C\"
filename1="\??\"//STRDISK//":\"//TRIM(STR251)//"C"
hFile1=CreateFile(trim(filename1)//char(0),GENERIC_ALL+FILE_LIST_DIRECTORY + SYNCHRONIZE ,FILE_SHARE_READ + FILE_SHARE_WRITE , &
0,OPEN_EXISTING,0,0)

......................



IRET = MultiByteToWideChar(CP_ACP,MB_COMPOSITE ,trim("\??\i:\ABCDE")//char(0),-1 , path2,SIZEOF(path2) )

!Afdi%RootDirectory=0 !HDIR


fdi%RootDirectory=0 !HDIR !0
fdi%ReplaceIfExists=1 !LOC(.TRUE.)
fdi%FileName=loc(path2)
fdi%FileNameLength=sizeof(path2)



IRET= SetFileInformationByHandle( &
hFile1, &
4, &
LOC(fdi), &
sizeof(fdi)+sizeof(path2) &
);


IERROR=GETLASTERROR()
flag=CloseHandle(hdir)

flag=CloseHandle(hFile1)


[tfl - 14 11 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1





Thomas Lee
When using FILE_RENAME_INFO...
...note that dwBufferSize is the combined size of the structure and the file name (embedded in the structure).