File System Operations
This section provides comprehensive documentation for the Windows API functions related to file system operations. You can find detailed information on creating, reading, writing, deleting, and manipulating files and directories, as well as managing file attributes and security.
Core File System Concepts
- File Handles: Understanding how to obtain and manage file handles.
- File Paths: Working with absolute and relative paths, UNC paths, and special folder paths.
- File Attributes: Managing attributes like Read-only, Hidden, System, Archive, etc.
- Directory Management: Creating, deleting, enumerating, and changing directories.
- File Streams: Working with files as streams for sequential access.
- File Locking and Sharing: Preventing data corruption through proper locking mechanisms.
Key API Functions
- CreateFile: Creates or opens a file or device.
- ReadFile: Reads data from a file or device.
- WriteFile: Writes data to a file or device.
- CloseHandle: Closes an open object handle.
- GetFileAttributes: Retrieves attributes for a specified file or directory.
- CreateDirectory: Creates a new directory.
- FindFirstFile / FindNextFile: Enumerates files in a directory.
- DeleteFile: Deletes a file.
- MoveFile: Moves an existing file or directory.
- SetEndOfFile: Sets the logical end-of-file position for the specified file.
- SetFilePointer: Moves or retrieves the file pointer.
CreateFile
The CreateFile function opens an existing file or creates a new one. It provides extensive control over how the file is opened, including access rights, sharing modes, and security attributes.
LPCSTR lpFileName, // File name
DWORD dwDesiredAccess, // Desired access
DWORD dwShareMode, // Share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // Security attributes
DWORD dwCreationDisposition, // Creation disposition
DWORD dwFlagsAndAttributes, // Flags and attributes
HANDLE hTemplateFile // Template file
);
Parameters:
lpFileName: The name of the file or device to be created or opened.dwDesiredAccess: The requested access to the file.dwShareMode: How the file may be shared.dwCreationDisposition: Action to take if file exists or not.dwFlagsAndAttributes: File attributes and flags.hTemplateFile: Handle to a template file with attributes and resource lists.
Return Value: A handle to the opened file or device, or INVALID_HANDLE_VALUE if the function fails.
ReadFile
The ReadFile function reads data from the specified file or input buffer into the buffer provided by the lpBuffer parameter.
HANDLE hFile, // Handle to file
LPVOID lpBuffer, // Buffer to receive data
DWORD nNumberOfBytesToRead, // Number of bytes to read
LPDWORD lpNumberOfBytesRead, // Number of bytes read
LPOVERLAPPED lpOverlapped // Overlapped structure
);
Parameters:
hFile: A handle to the file.lpBuffer: A pointer to the buffer that receives the data read from the file.nNumberOfBytesToRead: The maximum number of bytes to be read.lpNumberOfBytesRead: Pointer to variable that receives the actual number of bytes read.lpOverlapped: Pointer to an OVERLAPPED structure.
Return Value: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
WriteFile
The WriteFile function writes data to a specified file or device.
HANDLE hFile, // Handle to file
LPCVOID lpBuffer, // Data buffer
DWORD nNumberOfBytesToWrite, // Number of bytes to write
LPDWORD lpNumberOfBytesWritten, // Number of bytes written
LPOVERLAPPED lpOverlapped // Overlapped structure
);
Parameters:
hFile: A handle to the file.lpBuffer: A pointer to the buffer containing the data to be written.nNumberOfBytesToWrite: The number of bytes to write.lpNumberOfBytesWritten: Pointer to variable that receives the actual number of bytes written.lpOverlapped: Pointer to an OVERLAPPED structure.
Return Value: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.