File System Functions

This section provides comprehensive documentation for Windows API functions related to file system operations, including creating, reading, writing, deleting, and managing files and directories.

File Creation and Opening

CreateFile

Creates or opens a file or I/O device. It can create a new file, open an existing file, or open a device such as a disk drive, COM port, or printer.

HANDLE CreateFile(
  LPCSTR lpFileName,
  DWORD  dwDesiredAccess,
  DWORD  dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD  dwCreationDisposition,
  DWORD  dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters

  • lpFileName: LPCSTR - The name of the file or device to be created or opened.
  • dwDesiredAccess: DWORD - The type of access to the file or device for the specified file handle.
  • dwShareMode: DWORD - A bitmask that specifies how threads can access a file while it is held open by the current thread.
  • lpSecurityAttributes: LPSECURITY_ATTRIBUTES - A pointer to a SECURITY_ATTRIBUTES structure that contains security descriptor for the new file.
  • dwCreationDisposition: DWORD - An action to take if the file exists and what to do if it does not exist.
  • dwFlagsAndAttributes: DWORD - The file attributes and flags for the file or device.
  • hTemplateFile: HANDLE - A handle to a template file with the GENERIC_READ access right.

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.

OpenFile

Opens an existing file or creates a new file. This function is generally superseded by CreateFile.

int OpenFile(
  LPCSTR lpFileName,
  LPOVERLAPPED lpOverlapped,
  DWORD  dwFlags
);

File Reading and Writing

ReadFile

Reads data from a file or input device. The function starts at the position indicated by the file pointer and moves the file pointer by the number of bytes read.

BOOL ReadFile(
  HANDLE       hFile,
  LPVOID       lpBuffer,
  DWORD        nNumberOfBytesToRead,
  LPDWORD      lpNumberOfBytesRead,
  LPOVERLAPPED lpOverlapped
);

WriteFile

Writes data to a file or input device. This function starts writing data from the position indicated by the file pointer and, on the other side of the file, the file pointer is updated to point to the position after the last byte written.

BOOL WriteFile(
  HANDLE       hFile,
  LPCVOID      lpBuffer,
  DWORD        nNumberOfBytesToWrite,
  LPDWORD      lpNumberOfBytesWritten,
  LPOVERLAPPED lpOverlapped
);

File and Directory Management

SetFilePointer

Moves the file pointer of the specified file to a location within a file.

DWORD SetFilePointer(
  HANDLE hFile,
  LONG   lDistanceToMove,
  PLONG  lpDistanceToMoveHigh,
  DWORD  dwMoveMethod
);

CreateDirectory

Creates a new directory with the specified path. If the trailing backslash is omitted from this parameter, it is added automatically.

BOOL CreateDirectory(
  LPCSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpAttribute
);

RemoveDirectory

Marks a directory for deletion. Instead of removing the directory when the last handle to it is closed, RemoveDirectory marks it for deletion.

BOOL RemoveDirectory(
  LPCSTR lpPathName
);

GetFileAttributes

Retrieves the attributes of a specified file or directory.

DWORD GetFileAttributes(
  LPCSTR lpFileName
);

File Information

GetFileSizeEx

Retrieves the size of the specified file.

BOOL GetFileSizeEx(
  HANDLE         hFile,
  PLARGE_INTEGER lpFileSize
);

GetFileTime

Retrieves the date and time that a specified file was created, last accessed, and last modified.

BOOL GetFileTime(
  HANDLE       hFile,
  LPFILETIME lpCreationTime,
  LPFILETIME lpLastAccessTime,
  LPFILETIME lpLastWriteTime
);