File Management Functions
This section describes the Win32 API functions used for managing files and directories on Windows systems. These functions provide a robust set of tools for interacting with the file system, including creating, opening, reading, writing, and deleting files, as well as manipulating directories and their contents.
Creating and Opening Files
These functions allow you to create new files or open existing ones. You can specify various flags to control how the file is opened, such as whether it can be read from, written to, or if it should be created if it doesn't exist.
Function | Description |
---|---|
CreateFile | Creates or opens a file or I/O device. |
CreateFile2 | Creates or opens a file or I/O device. The CreateFile2 function is similar to the CreateFile function, but it has an additional parameter for specifying template file properties. |
OpenFile | Opens an existing file. This function is generally superseded by CreateFile . |
Reading and Writing Files
Once a file is open, you can use these functions to read data from it or write data to it. You can read data sequentially or at a specific position within the file.
Function | Description |
---|---|
ReadFile | Reads data from a specified file or input/output (I/O) device. |
WriteFile | Writes data to a specified file or I/O device. |
SetFilePointer | Moves the file pointer of the specified file to a location within the file. |
FlushFileBuffers | Flushes the buffers for a specified file and causes any buffered data to be written to the file. |
File Attributes and Properties
These functions allow you to get and set the attributes of files, such as their read-only status, archive bit, and compressed status. You can also retrieve information about the file's size, creation time, and modification time.
Function | Description |
---|---|
GetFileAttributes | Retrieves the attributes of a specified file or directory. |
SetFileAttributes | Sets the attributes of a specified file or directory. |
GetCompressedFileSize | Retrieves the uncompressed size of a compressed file. |
Directory Management
Manage directories with these functions. You can create new directories, remove existing ones, change the current directory, and find files within a directory.
Function | Description |
---|---|
CreateDirectory | Creates a new directory. |
RemoveDirectory | Removes an existing empty directory. |
GetCurrentDirectory | Retrieves the current directory for the calling process. |
FindFirstFile | Searches a directory for a file or subdirectory with a name that matches a specified string and retrieves information about that file or subdirectory. |
FindNextFile | Searches a directory for the next file or subdirectory that matches a specified set of attributes or a search string. |
FindClose | Closes the search handle and releases all system resources associated with the FindFirstFile or FindFirstFileEx function. |
File Locking
Control access to files to prevent data corruption when multiple processes might try to access the same file simultaneously.
Function | Description |
---|---|
LockFile | Locks a portion of a file. |
UnlockFile | Unlocks a portion of a file that was previously locked. |