BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Controlling File Attribute Flags

Microsoft Windows allows a number of Boolean attributes to be assigned to each file in the file system. These flags provide information such as indicating that a file is hidden or read-only. The properties can be read and changed using the .NET framework.

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:

ConstantDescription
ReadOnlySet if a file being examined is flagged as read-only. Such files cannot normally be edited.
HiddenSet for files that are hidden. These files are not displayed in folder listings, unless the user specifies that they should be visible.
SystemSet for files that are part of, or only for use by, the operating system. Deleting such files can cause stability problems.
DirectorySet for folders, not set for files.
ArchiveSet 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.
DeviceThis flag is reserved for future use.
NormalThis flag is set when no other attributes are present for a file. The flag cannot be combined with other constants from the enumeration.
TemporaryThe 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.
SparseFileSet for files that are flagged as sparse. These are large files that contain little information, having most of their data set to zero.
ReparsePointSet 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.
CompressedSet 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.
OfflineSet when a file is offline, indicating that its contents are not immediately available.
NotContentIndexedSet when a file has been excluded from indexing by the indexing service provided by Microsoft Windows.
EncryptedSet 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.

using System.IO;

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