Windows File System API Reference
This section provides comprehensive documentation for the Windows API functions related to file system operations. You can find details on creating, reading, writing, deleting, and managing files and directories, as well as handling file attributes and security permissions.
File Operations
Explore the functions for interacting with individual files:
Function | Description |
---|---|
CreateFile |
Creates or opens a file or I/O device. |
ReadFile |
Reads data from a file or I/O device. |
WriteFile |
Writes data to a file or I/O device. |
CloseHandle |
Closes an open object handle. |
DeleteFile |
Deletes a specified file. |
MoveFile |
Moves an existing file or directory. |
CopyFile |
Copies an existing file to a new location. |
Directory Operations
Manage directories and their contents:
Function | Description |
---|---|
CreateDirectory |
Creates a directory. |
RemoveDirectory |
Removes an empty directory. |
FindFirstFile / FindNextFile |
Enumerates files and directories in a specified directory. |
GetCurrentDirectory |
Retrieves the current working directory of the calling process. |
SetCurrentDirectory |
Changes the current working directory. |
File Attributes
Functions for managing file metadata and attributes:
Function | Description |
---|---|
GetFileAttributes |
Retrieves the file system attributes of a specified file or directory. |
SetFileAttributes |
Sets the file system attributes for a specified file or directory. |
Security
Understand how to manage access control for files and directories:
For detailed information on file security and access control lists (ACLs), please refer to the Security and Identity documentation.
Advanced Features
Explore more advanced file system functionalities:
- Asynchronous I/O: Learn about non-blocking file operations using Overlapped I/O.
- File Mapping: Understand how to map files into the address space of a process for efficient data access.
- Volume Management: Discover APIs for querying and managing disk volumes.
- Transacted Operations: Explore the possibility of atomic file system operations using Transactional NTFS (TxF).
Example: Reading a file
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFile;
char buffer[256];
DWORD bytesRead;
hFile = CreateFile(
"example.txt", // File name
GENERIC_READ, // Desired access
FILE_SHARE_READ, // Share mode
NULL, // Security attributes
OPEN_EXISTING, // Creation disposition
FILE_ATTRIBUTE_NORMAL, // Flags and attributes
NULL); // Template file
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error opening file: %lu\n", GetLastError());
return 1;
}
if (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
buffer[bytesRead] = '\0'; // Null-terminate the string
printf("File content:\n%s\n", buffer);
} else {
printf("Error reading file: %lu\n", GetLastError());
}
CloseHandle(hFile);
return 0;
}