Namespace: Windows API
Header: Winbase.h (include Windows.h)
Library: Kernel32.lib
BOOL SetFileAttributesW(
LPCWSTR lpFileName,
DWORD dwFileAttributes
);
| Name | Description |
|---|---|
lpFileName |
Pointer to a null-terminated string that specifies the name of the file or directory. Use the W version for Unicode strings. |
dwFileAttributes |
File attribute flags. Combine multiple attributes using the bitwise OR operator. Common values:
|
If the function succeeds, the return value is nonzero. If it fails, the return value is zero. To get extended error information, call GetLastError.
FILE_ATTRIBUTE_NORMAL attribute cannot be combined with any other attribute.FILE_ATTRIBUTE_READONLY attribute removes write permission for the file.FILE_ATTRIBUTE_ENCRYPTED attribute, the file becomes encrypted using EFS.SetFileAttributesA) and Unicode (SetFileAttributesW) variants. The ANSI version is less recommended for new development.
#include <windows.h>
#include <stdio.h>
int main(void)
{
LPCWSTR file = L"C:\\example\\test.txt";
// Add the Hidden attribute
if (SetFileAttributesW(file, FILE_ATTRIBUTE_HIDDEN))
wprintf(L"Successfully set hidden attribute on %s\\n", file);
else
wprintf(L"Failed to set attribute. Error: %lu\\n", GetLastError());
// Remove the Hidden attribute (set to normal)
if (SetFileAttributesW(file, FILE_ATTRIBUTE_NORMAL))
wprintf(L"Attributes cleared for %s\\n", file);
else
wprintf(L"Failed to clear attributes. Error: %lu\\n", GetLastError());
return 0;
}