SetAttributes Method

File.SetAttributes Method (String, FileAttributes)


Sets the specified FileAttributes of the file on the specified path.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static void SetAttributes(
	string path,
	FileAttributes fileAttributes


Type: System.String

The path to the file.

Type: System.IO.FileAttributes

A bitwise combination of the enumeration values.

Exception Condition

path is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.


path is in an invalid format.


The specified path is invalid, (for example, it is on an unmapped drive).


The file cannot be found.


path specified a file that is read-only.


This operation is not supported on the current platform.


path specified a directory.


The caller does not have the required permission.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Certain file attributes, such as Hidden and ReadOnly, can be combined. Other attributes, such as Normal, must be used alone.

It is not possible to change the compression status of a File object using the SetAttributes method.

For a list of common I/O tasks, see Common I/O Tasks.

The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

using System;
using System.IO;
using System.Text;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it does not exist.
        if (!File.Exists(path)) 

        FileAttributes attributes = File.GetAttributes(path);

        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
            // Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);

    private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
        return attributes & ~attributesToRemove;

for reading and writing files. Associated enumeration: FileIOPermissionAccess.Write

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2015 Microsoft