File Attribute Constants
This topic describes the file attribute constants that are used by various file I/O functions in the Win32 API.
Note: When specifying file attributes, you can combine multiple flags by using the bitwise OR operator (|).
Overview
File attributes are used to describe the characteristics of files and directories. These attributes can be set or retrieved using functions like CreateFile, GetFileAttributes, and SetFileAttributes. Understanding these constants is crucial for managing files programmatically on Windows.
Common File Attribute Constants
| Constant | Value (Hex) | Description |
|---|---|---|
FILE_ATTRIBUTE_READONLY |
0x00000001 |
The file or directory is read-only. Applications can only access the file or directory to read data. WriteFile and DeleteFile operations fail for this file. |
FILE_ATTRIBUTE_HIDDEN |
0x00000002 |
The file or directory is hidden. It is not included in an ordinary directory listing. |
FILE_ATTRIBUTE_SYSTEM |
0x00000004 |
The file or directory is a system file. The operating system uses this value to identify essential files. |
FILE_ATTRIBUTE_DIRECTORY |
0x00000010 |
This value indicates that the file is a directory. |
FILE_ATTRIBUTE_ARCHIVE |
0x00000020 |
The file or directory is an archive file or directory. Applications use this value to distinguish files that should be backed up or archived. |
FILE_ATTRIBUTE_ENCRYPTED |
0x00000040 |
This attribute and its index value are reserved for future use. |
FILE_ATTRIBUTE_NORMAL |
0x00000080 |
This attribute is for a file or directory that does not have any other attributes set. This attribute is valid only on the root of a drive, and then only if the file is not hidden, system, or archive. |
FILE_ATTRIBUTE_TEMPORARY |
0x00000100 |
The file is a temporary file. File systems avoid writing temporary files to disk. This flag helps optimize file I/O by keeping temporary files in memory. |
FILE_ATTRIBUTE_SPARSE_FILE |
0x00000200 |
The file is a sparse file. Sparse files are files that contain large blocks of zeros. |
FILE_ATTRIBUTE_REPARSE_POINT |
0x00000400 |
The file or directory is a reparse point, which is a block of user-defined data that can be used by a file system filter driver to provide more sophisticated file system functionality. |
FILE_ATTRIBUTE_COMPRESSED |
0x00000800 |
The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is inherited by newly created files and subdirectories. |
FILE_ATTRIBUTE_OFFLINE |
0x00001000 |
The data for the file is not immediately available. This attribute indicates that the file data is archived remotely. |
FILE_ATTRIBUTE_DATA_ENCRYPTED |
0x00008000 |
The file or directory is encrypted using the Encrypting File System (EFS). |
FILE_ATTRIBUTE_INTEGRITY_STREAM |
0x00004000 |
This attribute is reserved for future use. |
FILE_ATTRIBUTE_NO_SCRUB_DATA |
0x00008000 |
This attribute is reserved for future use. |
Example Usage (Conceptual C++)
Here's a conceptual example of how you might use these constants in C++:
#include <windows.h>
HANDLE hFile = CreateFile(
L"MyFile.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
// Handle error
DWORD error = GetLastError();
} else {
// File opened successfully
// ... read from file ...
CloseHandle(hFile);
}