C# – How to make a file read-only

There are two ways to programmatically make a file read-only:

  • Set FileInfo.IsReadOnly to true.
  • Use File.SetAttribute() and bitwise OR the file’s current attributes with FileAttributes.ReadOnly.

Here’s an example showing both ways to make a file read-only:

using System.IO;

var filePath = @"C:\HelloWorld\c.txt";

//Option 1 
new FileInfo(filePath).IsReadOnly = true;

//Option 2
var currentAttributes = File.GetAttributes(filePath);
File.SetAttributes(filePath, currentAttributes | FileAttributes.ReadOnly);
Code language: C# (cs)

Using File.SetAttributes() is useful when you want to manage all of file’s attributes at once. FileAttributes is an enum flag, so you can use bitwise operations to add/remove multiple attributes at once. That’s why you have to bitwise OR the read-only flag with the current attributes – otherwise the current attributes are completely replaced. In other words, if you use File.SetAttributes(file, FileAttributes.ReadOnly), it removes the file’s current attributes and only sets the read-only attribute.

Using FileInfo.IsReadOnly is simpler, so that’s what I suggest using. In the examples below, I’ll use this simpler approach when showing how to solve problems.

Remove the read-only attribute

Once a file is read-only, you’ll get an exception when you try to write to it or delete it:

System.UnauthorizedAccessException: Access to the path ‘C:\HelloWorld\c.txt’ is denied.

You can remove the read-only attribute by setting FileInfo.IsReadOnly to false. Then you can write to the file (or delete it) without a problem. Here’s an example:

using System.IO;

var filePath = @"C:\HelloWorld\c.txt";

new FileInfo(filePath).IsReadOnly = false;

File.AppendAllText(filePath, "I can write!");
Code language: C# (cs)

Change the read-only status for all files in a directory

You can change the read-only status for all files in a directory (including in its subdirectories) by looping through the files and setting FileInfo.IsReadOnly.

Here’s an example. Let’s say you want to delete a directory that contains read-only files. Since you’re deleting the directory recursively (by removing all of its files and subdirectories), when it tries to delete a read-only file, it’ll throw an exception (same one mentioned in the previous section). You have to remove the read-only attribute from all files in order to delete the directory.

Here’s an example of looping through all files in a directory and removing the read-only attribute by setting FileInfo.IsReadOnly to false, therefore enabling the directory to be deleted:

using System.IO;

var dirPath = @"C:\HelloWorld\";

//Set all of a directory's files to read-only (including in all subdirectories)
foreach(var filePath in Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories))
{
    new FileInfo(filePath).IsReadOnly = false;
}

//Now the directory can be deleted
Directory.Delete(dirPath, recursive: true);
Code language: C# (cs)

Note: You can also use DirectoryInfo.GetFiles() to loop through the FileInfo objects, but that’s a little bit more verbose.

You can use the same technique for making all of a directory’s files read-only.

Leave a Comment