In C#, File.SetAttributes() changes the FileAttributes of a file. The most common use is turning ReadOnly on or off. The value you pass replaces the old flags; it is not merged with them.
That is why the usual pattern is read, change, write: get the current flags, add a flag with | or remove one with & ~, and pass the result back.
This is a static method of System.IO.File. Call it as File.SetAttributes(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Read the current flags, change them, and set them again.
Lock it. The current flags are read, ReadOnly is added with |, and the result is set. Nothing else about the file changes.
The write is refused. Saving over a read-only file throws UnauthorizedAccessException, the catch block prints a message, and the content stays v1. Run the program as a normal user: an administrator or root account may bypass the lock on Linux and macOS.
Unlock it.& ~FileAttributes.ReadOnly clears just that one bit. ~ flips all the bits of the flag, and & keeps only the bits that are still set in both values.
Now it works. The write succeeds and the file reads v3. The folder cleanup at the end also works, because no read-only file is left.
Example 2 — Add and Remove a Flag with | and & ~
Print the ReadOnly state after each change to see what the bit operators do.
A tiny helper.IsReadOnly reads the flags fresh each time, so every print shows what is really on disk and not a stale variable.
|= adds a flag.attrs |= FileAttributes.ReadOnly turns the ReadOnly bit on. SetAttributes then writes the whole value to the file.
&= ~ removes a flag.attrs &= ~FileAttributes.ReadOnly turns the same bit off and leaves every other bit alone.
The variable is only a copy. Changing attrs does nothing to the file until SetAttributes is called. That is the step beginners most often forget.
Example 3 — FileAttributes.Normal Clears Everything
Set only ReadOnly, then set Normal and print the result.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-setattributes-normal");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "plain.txt");
File.WriteAllText(path, "demo");
File.SetAttributes(path, FileAttributes.ReadOnly);
Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly));
File.SetAttributes(path, FileAttributes.Normal);
Console.WriteLine(File.GetAttributes(path).HasFlag(FileAttributes.ReadOnly));
Console.WriteLine(File.GetAttributes(path));
Directory.Delete(root, true);
}
}
Output
True
False
Normal
How It Works
Replace, do not merge. Passing FileAttributes.ReadOnly alone sets exactly that one flag. Any Archive or Hidden flag the file had is dropped.
Normal is the reset button.FileAttributes.Normal means "no special flags". It is allowed only on its own, and it also clears ReadOnly.
Printing the value. An enum prints its name, so a file with no flags shows Normal. This is the usual way to make a read-only file deletable again.
Frequently Asked Questions
No. It replaces them. Read the current value with GetAttributes, change it with | or & ~, and then call SetAttributes with the result.
Call File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly). You can also set FileInfo.IsReadOnly = true.
Call File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly), or set FileAttributes.Normal if no other flag matters.
On Windows File.Delete throws UnauthorizedAccessException for a read-only file. Clear the ReadOnly flag first, then delete.
Not in the same way. On Linux a file is hidden when its name starts with a dot, and attributes such as Hidden, System, or Archive are not stored. Rely on ReadOnly for portable code.