File Attributes [C#]
This example shows how to get or set file attributes, how to add attributes and how to remove attributes from current ones.
Get file attributes
To get file attributes use static method File.GetAttributes. The method returns FileAttributes which is a bitwise combination of file attribute flags.
[C#]string filePath = @"c:\test.txt"; // get file attributes FileAttributes fileAttributes = File.GetAttributes(filePath);
Set file attributes
To set file attributes use static method File.SetAttributes. Parameter of the method is a bitwise combination of FileAttributes enumeration.
[C#]// clear all file attributes File.SetAttributes(filePath, FileAttributes.Normal); // set just only archive and read only attributes (no other attribute will set) File.SetAttributes(filePath, FileAttributes.Archive | FileAttributes.ReadOnly);
Check whether a file has any attribute
To check whether a file has any attribute (readonly, hidden) get current file attributes first and use bitwise AND (&) operator with a mask of specific attributes.
[C#]// check whether a file is read only bool isReadOnly = ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly); // check whether a file is hidden bool isHidden = ((File.GetAttributes(filePath) & FileAttributes.Hidden) == FileAttributes.Hidden); // check whether a file has archive attribute bool isArchive = ((File.GetAttributes(filePath) & FileAttributes.Archive) == FileAttributes.Archive); // check whether a file is system file bool isSystem = ((File.GetAttributes(filePath) & FileAttributes.System) == FileAttributes.System);
Add file attributes to current ones
To add file attributes to the existing ones get the current file attributes first and use bitwise OR (|) with the desired attributes.
[C#]// set (add) hidden attribute (hide a file) File.SetAttributes(filePath, File.GetAttributes(filePath) | FileAttributes.Hidden); // set (add) both archive and read only attributes File.SetAttributes(filePath, File.GetAttributes(filePath) | (FileAttributes.Archive | FileAttributes.ReadOnly));
Delete/clear file attributes from current ones
To delete file attributes from the existing ones get the current file attributes first and use AND (&) operator with a mask (bitwise complement of desired attributes combination).
[C#]// delete/clear hidden attribute File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.Hidden); // delete/clear archive and read only attributes File.SetAttributes(filePath, File.GetAttributes(filePath) & ~(FileAttributes.Archive | FileAttributes.ReadOnly));
Files and Folders Examples
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Read Text File – how to read lines from text file
- [C#] Load Text File to String – how to load text from file to string
- [C#] Get Files from Directory – how to get files from directory
- [C#] Delete All Files – how to delete files from the specified directory
- [C#] Get Application Directory – how to get application or assembly folder
- [C#] File Attributes – how to get or set file attributes
- [C#] Get File Time – how to get last modification time of a file
- [C#] Open File With Associated Application – how to launch the default application
See also
- FileAttributes – MSDN – enumeration of file attributes (ReadOnly, Hidden, …)
- File.GetAttributes – MSDN – method to get file attributes
- File.SetAttributes – MSDN – method to set file attributes