File.GetAttributes Method

Definition

Overloads

GetAttributes(SafeFileHandle)

Gets the specified FileAttributes of the file or directory associated with fileHandle.

GetAttributes(String)

Gets the FileAttributes of the file on the path.

GetAttributes(SafeFileHandle)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Gets the specified FileAttributes of the file or directory associated with fileHandle.

public static System.IO.FileAttributes GetAttributes(Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);

Parameters

fileHandle
SafeFileHandle

A SafeFileHandle to the file or directory for which the attributes are to be retrieved.

Returns

The FileAttributes of the file or directory.

Exceptions

fileHandle is null.

The caller does not have the required permission.

Applies to

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

GetAttributes(String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Gets the FileAttributes of the file on the path.

public static System.IO.FileAttributes GetAttributes(string path);

Parameters

path
String

The path to the file.

Returns

The FileAttributes of the file on the path.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is empty, contains only white spaces, or contains invalid characters.

The specified path, file name, or both exceed the system-defined maximum length.

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.

Examples

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))
        {
            File.Create(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);
        }
        else
        {
            // 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;
    }
}

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0