FileAttributes Enumeration
Every file and folder within the file system of a computer running a Microsoft Windows operating system includes a number of flags that describe its behaviour. These include attributes that determine whether a file is read-only, hidden or a system file, as well as many others. When you are developing .NET-based software that works with the file system, you can read these flags and use the information to determine how information should be displayed to a user. You can also modify some of the flags to change the way that the files act.
When you work with the Boolean file attributes, the information that describes all of the possible flags is combined into a single value. This is held as a FileAttributes value. The FileAttributes type is an enumeration that is decorated with the Flags attribute to make it a bit field. Each of the file property flags is represented by a constant in the enumeration and those constants can be combined using logical bitwise operations.
The enumeration contains the following values:
Constant | Description |
---|
ReadOnly | Set if a file being examined is flagged as read-only. Such files cannot normally be edited. |
Hidden | Set for files that are hidden. These files are not displayed in folder listings, unless the user specifies that they should be visible. |
System | Set for files that are part of, or only for use by, the operating system. Deleting such files can cause stability problems. |
Directory | Set for folders, not set for files. |
Archive | Set for files that are ready to be archived. This flag is set automatically by the operating system when files are updated. It is usually cleared by backup software, which can use the flag to determine if a file should be included in an incremental backup process. |
Device | This flag is reserved for future use. |
Normal | This flag is set when no other attributes are present for a file. The flag cannot be combined with other constants from the enumeration. |
Temporary | The file is temporary. For such files the operating system will try to store the file's information in memory, rather than writing it to disk, to improve performance. Temporary files are generally deleted by applications when no longer required. |
SparseFile | Set for files that are flagged as sparse. These are large files that contain little information, having most of their data set to zero. |
ReparsePoint | Set for files that are reparse points. These files are blocks of data associated with another file or folder, allowing custom metadata to be held for a file. |
Compressed | Set for files that are compressed to save disk space. This flag cannot be set for a file using the method described later in this article. |
Offline | Set when a file is offline, indicating that its contents are not immediately available. |
NotContentIndexed | Set when a file has been excluded from indexing by the indexing service provided by Microsoft Windows. |
Encrypted | Set for files that have been encrypted for additional security, and for folders where new files will be encrypted by default. |
The FileAttributes enumeration and the other class that we will be using are found in the System.IO namespace, so the following using directive should be included in your code.
Reading File Attributes
To retrieve the flags for a file or folder you can use the File class's static method, GetAttributes. This method has a single parameter that receives a string containing the path of the file or folder to examine. It returns a FileAttributes value, from which you can extract individual flags using logical bitwise operators or the enumeration's HasFlag method, when using later versions of the .NET framework.
FileAttributes attributes = File.GetAttributes(@"c:\test\test.txt");
bool readOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
Changing File Attributes
To change the attributes of a file or folder, create an appropriate FileAttributes value and pass this, along with the path for the target item, to the SetAttributes method. This is demonstrated in the code below. Here we read the attributes of a file, toggle the read-only flag with the exclusive-or operator and set the file's attributes to the new value.
FileAttributes attributes = File.GetAttributes(@"c:\test\test.txt");
attributes = attributes ^ FileAttributes.ReadOnly;
File.SetAttributes(@"c:\test\test.txt", attributes);
12 Jul 2012