File System Operations
This section details the core Windows API functions for interacting with the file system. These functions allow you to create, read, write, delete, and manage files and directories.
Core File Functions
Creating and Opening Files
To work with files, you typically need to open them first. The following functions are commonly used:
| Function Name | Description | Relevant Parameters |
|---|---|---|
CreateFile |
Creates or opens a file or I/O device. | lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile |
OpenFile |
Opens an existing file. | lpFileName, lpReOpenBuf, uStyle |
Reading and Writing Data
Once a file is open, you can read from or write to it using these functions:
| Function Name | Description | Key Parameters |
|---|---|---|
ReadFile |
Reads data from a file or from the communications resource that is associated with the specified file handle. | hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped |
WriteFile |
Writes data to a file or to the communications resource that is associated with the specified file handle. | hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, lpOverlapped |
SetFilePointer |
Moves the file pointer of the specified file to a location within the file. | hFile, lDistanceToMove, lpNewFilePointer, dwMoveMethod |
Closing Files
It is crucial to close file handles when you are finished with them to release resources and ensure data is flushed to disk.
| Function Name | Description |
|---|---|
CloseHandle |
Closes an open object handle. |
Directory Operations
Manage directories with the following functions:
| Function Name | Description |
|---|---|
CreateDirectory |
Creates a new directory. |
RemoveDirectory |
Removes an existing empty directory. |
GetCurrentDirectory |
Retrieves the current working directory of the calling process. |
SetCurrentDirectory |
Changes the current working directory of the calling process. |
File Attributes and Information
Query and modify file attributes and retrieve detailed file information:
| Function Name | Description |
|---|---|
GetFileAttributes |
Retrieves the attributes of a specified file or directory. |
SetFileAttributes |
Sets the attributes of a specified file or directory. |
GetFileSizeEx |
Retrieves the size of the specified file. |
Example Usage (Conceptual)
Here's a simplified C++ snippet illustrating the use of CreateFile and WriteFile:
#include <windows.h>
#include <iostream>
int main() {
HANDLE hFile = CreateFile(
L"example.txt", // File name
GENERIC_WRITE, // Desired access
0, // Share mode
NULL, // Security attributes
CREATE_ALWAYS, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // Flags and attributes
NULL // Template file
);
if (hFile == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create/open file. Error: " << GetLastError() << std::endl;
return 1;
}
const char* data = "Hello, Windows API!";
DWORD bytesWritten;
if (!WriteFile(hFile, data, strlen(data), &bytesWritten, NULL)) {
std::cerr << "Failed to write to file. Error: " << GetLastError() << std::endl;
} else {
std::cout << "Successfully wrote " << bytesWritten << " bytes to example.txt" << std::endl;
}
CloseHandle(hFile);
return 0;
}