Windows API Reference

Core Functions - File Management

File Management Functions (FileApi.h)

This section details the Win32 API functions used for file and directory operations, including creating, reading, writing, deleting, and managing file system objects.

Core Functions

CreateFile

HANDLE CreateFile( LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );

Description

Creates or opens a file or I/O device. It returns a handle that can be used to access the file or device.

Parameters

Parameter Description
lpFileName The name of the file or device.
dwDesiredAccess The access to the file or device. This can be read, write, or both.
dwShareMode The sharing mode of an object, which is the combination of zero or more of the following flags.
lpSecurityAttributes A pointer to a SECURITY_ATTRIBUTES structure that specifies the security descriptor for the object.
dwCreationDisposition Determines whether the file is created or opened.
dwFlagsAndAttributes The file system attributes and flags for the file.
hTemplateFile A handle to a template file with the extended attributes that can be applied to the function.

Return Value

If the function succeeds, the return value is an open handle to the specified file or device. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

ReadFile

BOOL ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped );

Description

Reads data from a file or overlapping I/O device. The position is set by the position indicator of the file handle.

Parameters

Parameter Description
hFile A handle to the file or device (CreateFile).
lpBuffer A pointer to the buffer that receives the data read from the file.
nNumberOfBytesToRead The maximum number of bytes to be read.
lpNumberOfBytesRead A pointer to a variable that receives the number of bytes actually read.
lpOverlapped A pointer to an OVERLAPPED structure, used for asynchronous operations.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

WriteFile

BOOL WriteFile( HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped );

Description

Writes data to a file or I/O device. The position is set by the position indicator of the file handle.

Parameters

Parameter Description
hFile A handle to the file or device (CreateFile).
lpBuffer A pointer to the buffer containing the data to be written.
nNumberOfBytesToWrite The number of bytes to write.
lpNumberOfBytesWritten A pointer to a variable that receives the number of bytes actually written.
lpOverlapped A pointer to an OVERLAPPED structure, used for asynchronous operations.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

CloseHandle

BOOL CloseHandle( HANDLE hObject );

Description

Closes an open object handle. For files, this releases the handle and flushes any buffered data.

Parameters

Parameter Description
hObject A handle to an open object such as a file, a pipe, or a process.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

SetFilePointer

DWORD SetFilePointer( HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod );

Description

Moves the file pointer of the specified file to a location within the file.

Parameters

Parameter Description
hFile A handle to the file.
lDistanceToMove The number of bytes to move the file pointer.
lpDistanceToMoveHigh A pointer to a signed 32-bit value that contains the high-order bits of the distance to move.
dwMoveMethod The starting point for the move.

Return Value

If the function succeeds, the return value is the low-order 32 bits of the new file pointer. If the function fails, the return value is 0xFFFFFFFF.

GetFileSizeEx

BOOL GetFileSizeEx( HANDLE hFile, PLARGE_INTEGER lpFileSize );

Description

Retrieves the size of the specified file.

Parameters

Parameter Description
hFile A handle to the file.
lpFileSize A pointer to a LARGE_INTEGER structure that receives the file size.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

CreateDirectory

BOOL CreateDirectory( LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes );

Description

Creates a new directory, including any necessary parent directories.

Parameters

Parameter Description
lpPathName The path of the directory to create.
lpSecurityAttributes A pointer to a SECURITY_ATTRIBUTES structure.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

RemoveDirectory

BOOL RemoveDirectory( LPCSTR lpPathName );

Description

Deletes an existing empty directory.

Parameters

Parameter Description
lpPathName The path of the directory to be removed.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

DeleteFile

BOOL DeleteFile( LPCSTR lpFileName );

Description

Deletes a specified file.

Parameters

Parameter Description
lpFileName The name of the file to be deleted.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

CopyFile

BOOL CopyFile( LPCSTR lpExistingFileName, LPCSTR lpNewFileName, BOOL bFailIfExists );

Description

Copies an existing file to a new location.

Parameters

Parameter Description
lpExistingFileName The name of the existing file.
lpNewFileName The name of the new file.
bFailIfExists If this parameter is TRUE and the new file already exists, the function will fail.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

MoveFile

BOOL MoveFile( LPCSTR lpExistingFileName, LPCSTR lpNewFileName );

Description

Moves an existing file from one directory to another or renames a file.

Parameters

Parameter Description
lpExistingFileName The current name of the file.
lpNewFileName The new name for the file.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

FindFirstFile

HANDLE FindFirstFile( LPCSTR lpFindFileData, LPWIN32_FIND_DATAA lpFindData );

Description

Begins the process of searching for files that match a specific pattern.

Parameters

Parameter Description
lpFindFileData A pointer to a null-terminated string that specifies a valid file path or directory. The string can contain wildcard characters (* and ?).
lpFindData A pointer to a WIN32_FIND_DATA structure that receives information about a found file or directory.

Return Value

If the function succeeds, the return value is a handle used by the FindNextFile function to retrieve subsequent files. If the function fails, the return value is INVALID_HANDLE_VALUE.

FindNextFile

BOOL FindNextFile( HANDLE hFindFile, LPWIN32_FIND_DATAA lpFindFileData );

Description

Continues a file search started by a call to the FindFirstFile function.

Parameters

Parameter Description
hFindFile A handle to the file search returned by a previous call to FindFirstFile.
lpFindFileData A pointer to a WIN32_FIND_DATA structure that receives information about the next file.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. If no more files can be found, the return value is zero and GetLastError returns ERROR_NO_MORE_FILES.

FindClose

BOOL FindClose( HANDLE hFindFile );

Description

Closes the search handle opened by a previous call to FindFirstFile.

Parameters

Parameter Description
hFindFile A handle to the file search returned by FindFirstFile.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

GetFileInformationByHandle

BOOL GetFileInformationByHandle( HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation );

Description

Retrieves specified information about an open file.

Parameters

Parameter Description
hFile A handle to the file.
lpFileInformation A pointer to a BY_HANDLE_FILE_INFORMATION structure that receives the information.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.