GetAttributes Method

File.GetAttributes Method (String)


Gets the FileAttributes of the file on the path.

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

public static FileAttributes GetAttributes(
	string path


Type: System.String

The path to the file.

Return Value

Type: System.IO.FileAttributes

The FileAttributes of the file on the path.

Exception Condition

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


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.


path represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.


path represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.


This file is being used by another process.


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.

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 files. Associated enumeration: FileIOPermissionAccess.Read

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Available since 4.0
Return to top
© 2015 Microsoft