File Management Functions
This section covers the Windows API functions related to file and directory management. These functions allow applications to create, open, read, write, delete, and query information about files and directories.
Core File Operations
- 📄 CreateFile
- 📖 ReadFile
- ✍️ WriteFile
- ❌ DeleteFile
- 🔎 GetFileAttributes
- 🚀 CopyFile
- ✂️ MoveFile
Directory Management
File Information and Control
CreateFile
Creates or opens a file or I/O device. It returns a handle that can be used to access the file.
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Parameters:
| Name | Type | Description |
|---|---|---|
lpFileName |
LPCTSTR |
The name of the file or device. |
dwDesiredAccess |
DWORD |
The access to the file. Can be GENERIC_READ, GENERIC_WRITE, etc. |
dwShareMode |
DWORD |
The sharing mode of the object. |
lpSecurityAttributes |
LPSECURITY_ATTRIBUTES |
Security descriptor. |
dwCreationDisposition |
DWORD |
Specifies how to create or open the file. |
dwFlagsAndAttributes |
DWORD |
File attributes and flags. |
hTemplateFile |
HANDLE |
Template file for attributes. |
GetLastError to retrieve error information.
ReadFile
Reads data from a file or I/O device. It fills a buffer with data read from the specified file.
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
Parameters:
| Name | Type | Description |
|---|---|---|
hFile |
HANDLE |
Handle to the file to be read. |
lpBuffer |
LPVOID |
Buffer that receives the data. |
nNumberOfBytesToRead |
DWORD |
The maximum number of bytes to be read. |
lpNumberOfBytesRead |
LPDWORD |
Number of bytes actually read. |
lpOverlapped |
LPOVERLAPPED |
Overlapped structure for asynchronous operations. |
WriteFile
Writes data to a file or I/O device. Writes data from a buffer into the specified file.
BOOL WriteFile(
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
Parameters:
| Name | Type | Description |
|---|---|---|
hFile |
HANDLE |
Handle to the file to be written to. |
lpBuffer |
LPCVOID |
Buffer containing the data to be written. |
nNumberOfBytesToWrite |
DWORD |
Number of bytes to write. |
lpNumberOfBytesWritten |
LPDWORD |
Number of bytes actually written. |
lpOverlapped |
LPOVERLAPPED |
Overlapped structure for asynchronous operations. |
DeleteFile
Deletes a specified file.
BOOL DeleteFile(
LPCTSTR lpFileName
);
Parameters:
| Name | Type | Description |
|---|---|---|
lpFileName |
LPCTSTR |
The name of the file to be deleted. |
GetFileAttributes
Retrieves the attributes of a specified file or directory.
DWORD GetFileAttributes(
LPCTSTR lpFileName
);
Parameters:
| Name | Type | Description |
|---|---|---|
lpFileName |
LPCTSTR |
The name of the file or directory. |
Returns a bitmask of attribute flags (e.g., FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_READONLY) or INVALID_FILE_ATTRIBUTES on failure.
CreateDirectory
Creates a new directory with the specified path.
BOOL CreateDirectory(
LPCTSTR lpPathName,
LPSECURITY_ATTRIBUTES lpAttribute
);
Parameters:
| Name | Type | Description |
|---|---|---|
lpPathName |
LPCTSTR |
The path of the directory to create. |
lpAttribute |
LPSECURITY_ATTRIBUTES |
Security descriptor. |
FindFirstFile / FindNextFile
FindFirstFile searches a directory for a file or subdirectory with a name that matches a specified pattern. FindNextFile continues a directory search.
HANDLE FindFirstFile(
LPCTSTR lpFileName,
LPWIN32_FIND_DATA lpFindFileData
);
BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);
Parameters:
| Name | Type | Description |
|---|---|---|
lpFileName |
LPCTSTR |
The directory or path and file name pattern. |
lpFindFileData |
LPWIN32_FIND_DATA |
Receives information about the found file or directory. |
hFindFile |
HANDLE |
Handle returned by FindFirstFile. |
FindClose on the handle returned by FindFirstFile when done.