C# File GetAttributes() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.GetAttributes() returns a FileAttributes value that describes a file or folder. It is a flags enum, so one value can hold several settings at once, such as ReadOnly and Hidden.

Test one flag with HasFlag or with the bit test (attrs & FileAttributes.ReadOnly) != 0. The method also works for folders, which is a common way to ask is this path a file or a folder?.

This is a static method of System.IO.File. Call it as File.GetAttributes(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Read the value once, then test the flags you care about.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-getattributes.txt");
File.WriteAllText(path, "demo");

FileAttributes attrs = File.GetAttributes(path);

Console.WriteLine(attrs.HasFlag(FileAttributes.ReadOnly));
CallWhat it does
File.GetAttributes(path)Returns the attribute flags of the file or folder.
attrs.HasFlag(FileAttributes.ReadOnly)true when the ReadOnly flag is set.
(attrs & FileAttributes.Directory) != 0The same test written with a bit mask. It is true for folders.

FileInfo.Attributes gives the same flags as a property. On Windows many flags exist (Archive, Hidden, System, Encrypted); on Linux and macOS only a few, such as ReadOnly, Directory, and Hidden for dot-files, are meaningful.

Library declarations
C#
public static FileAttributes GetAttributes(string path);

Parameters

ParameterDescription
pathThe file or folder to inspect. It must exist.

Return Value

Return ValueDescription
FileAttributesA combination of flags. A plain file with no other flags returns Normal. A folder includes Directory.

C# Examples

Example 1 — Check ReadOnly and Directory on a New File

Read the attributes of a file you just created and test two flags.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getattributes");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "notes.txt");
        File.WriteAllText(path, "demo");

        FileAttributes attrs = File.GetAttributes(path);

        Console.WriteLine("ReadOnly: " + attrs.HasFlag(FileAttributes.ReadOnly));
        Console.WriteLine("Is folder: " + ((attrs & FileAttributes.Directory) == FileAttributes.Directory));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A fresh file. WriteAllText creates an ordinary file with no read-only setting.
  2. One read, many tests. GetAttributes is called once and the value is kept in attrs, so both tests use the same snapshot.
  3. Two ways to test. HasFlag reads clearly. The mask (attrs & flag) == flag does the same job and is the older style. Both flags are off, so the output is False twice.
  4. Safe to print everywhere. The example prints only these two flags, because the full list of flags differs between Windows and Linux.

Example 2 — Tell a File from a Folder

Use the Directory flag to learn what a path points to.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getattributes-kind");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string file = Path.Combine(root, "a.txt");
        File.WriteAllText(file, "x");

        Console.WriteLine(File.GetAttributes(root).HasFlag(FileAttributes.Directory));
        Console.WriteLine(File.GetAttributes(file).HasFlag(FileAttributes.Directory));

        Directory.Delete(root, true);
    }
}

How It Works

  1. One method, two kinds of path. Despite the class name, File.GetAttributes accepts a folder path as well as a file path.
  2. A folder has the Directory flag. The first line passes root, a folder, so HasFlag(Directory) is True.
  3. A file does not. The second line passes a.txt and prints False.
  4. A handy trick. Unlike File.Exists and Directory.Exists, this works when you do not know the kind of path in advance, as long as the path exists.

Example 3 — Watch a Flag Appear and Disappear

Set the ReadOnly flag, read it back, remove it, and also see the missing-file error.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-getattributes-ro");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "locked.txt");
        File.WriteAllText(path, "data");

        FileAttributes original = File.GetAttributes(path);

        File.SetAttributes(path, original | FileAttributes.ReadOnly);
        Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly));

        File.SetAttributes(path, original);
        Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly));

        File.Delete(path);
        try {
            File.GetAttributes(path);
        } catch (FileNotFoundException) {
            Console.WriteLine("No such file");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Remember the start. original keeps the flags the file had at first, so they can be restored exactly.
  2. Add one flag. original | FileAttributes.ReadOnly turns on ReadOnly and keeps every other flag. GetAttributes then reports True.
  3. Restore. Setting original again removes the flag, so the next read prints False. A read-only file would block the cleanup, so always restore.
  4. Missing means exception. After Delete, GetAttributes throws FileNotFoundException. Unlike Exists, it does not return a quiet answer.

Frequently Asked Questions

A FileAttributes value. It is a flags enum, so one value can combine ReadOnly, Hidden, Archive, Directory, and more.
Use File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly), or FileInfo.IsReadOnly. Do not compare the whole value with ==.
Yes. The result includes the Directory flag, which makes it a quick way to find out whether a path is a folder.
Normal means no other flags are set. It is common on Linux and macOS. On Windows a new file usually has Archive set instead.
GetAttributes throws FileNotFoundException for a missing file or DirectoryNotFoundException for a missing folder. Check with File.Exists first if that is possible.

Next: GetCreationTime()

Read when a file was created, as a local DateTime.

GetCreationTime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful