File System Core APIs
This section details the core Windows APIs for interacting with the file system. These APIs provide fundamental operations for managing files, directories, and volumes.
File and Directory Operations
-
CreateFile
Creates or opens a file or I/O device. It returns a handle that can be used to access the file or device.
-
ReadFile
Reads data from a file or I/O device. It fills a buffer with data read from the specified file or device.
-
WriteFile
Writes data to a file or I/O device. It writes the specified buffer to the specified file or device.
-
CloseHandle
Closes an open object handle. This is crucial for releasing resources.
-
GetFileAttributes
Retrieves attributes for a specified file or directory.
-
SetFileAttributes
Sets the attributes for a specified file or directory.
-
DeleteFile
Deletes a specified file.
-
CreateDirectory
Creates a new directory.
-
RemoveDirectory
Removes an empty directory.
-
CopyFile
Copies an existing file to a new location.
-
MoveFile
Moves an existing file or directory from one location to another.
Directory Enumeration
-
FindFirstFile
Begins the enumeration of a directory or the file system.
-
FindNextFile
Continues a directory or file system search.
-
FindClose
Closes the search handle supplied by a previous call to FindFirstFile or FindNextFile.
Volume Information
-
GetLogicalDrives
Retrieves a bitmask of the currently available disk drives.
-
GetDriveType
Determines the type of the specified disk drive.
-
GetVolumeInformation
Retrieves information about a file system and a specified volume to include volume label, file system name, and serial number.
CreateFile Function
Syntax
HANDLE CreateFile(
LPCSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);
Parameters
lpFileName: The name of the file or device to be created or opened.dwDesiredAccess: The requested access to the file or device.dwShareMode: A bitmap of the modes that wish to share an Handle to an object with other threads.lpSecurityAttributes: A pointer to a SECURITY_ATTRIBUTES structure that contains security information for the file or directory.dwCreationDisposition: An action to take if the file exists, and what to do if it does not exist.dwFlagsAndAttributes: The file system attributes and flags for the file or directory.hTemplateFile: A handle to the template file for the file being created.
Return Value
If the function succeeds, the return value is an open handle to the specified file or device. If the function fails, the return value is INVALID_HANDLE_VALUE.
ReadFile Function
Syntax
BOOL ReadFile(
HANDLE hFile,
LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped
);
Parameters
hFile: A handle to the file or device (CreateFile returns this handle).lpBuffer: A buffer that receives the data read from the specified file or device.nNumberOfBytesToRead: The maximum number of bytes to be read.lpNumberOfBytesRead: A pointer to a variable that receives the number of bytes read.lpOverlapped: A 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 Function
Syntax
BOOL WriteFile(
HANDLE hFile,
LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped
);
Parameters
hFile: A handle to the file or device (CreateFile returns this handle).lpBuffer: A pointer to a buffer that contains the data to be written to the specified file or device.nNumberOfBytesToWrite: The number of bytes to be written to the file or device.lpNumberOfBytesWritten: A pointer to a variable that receives the number of bytes written.lpOverlapped: A 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.
FindFirstFile Function
Syntax
HANDLE FindFirstFile(
LPCSTR lpFileName,
LPWIN32_FIND_DATAA lpFindFileData
);
Parameters
lpFileName: The root directory and the path for a file or for a set of files.lpFindFileData: A pointer to a WIN32_FIND_DATA structure that receives information about a found file or directory.
Return Value
If the function succeeds, the return value is a search handle used by subsequent calls to the FindNextFile function. If the function fails, the return value is INVALID_HANDLE_VALUE.