This article explains how to set the “Read-Only” attribute via code for important configuration files or data that should not be accidentally overwritten or deleted. It also covers how to remove the attribute when you need to edit the file. In C#, you can easily manage this by toggling the IsReadOnly property of the FileInfo class between true and false.
Implementation Sample: Protecting Important Data with a Lock
The following code checks the state of a confidential data file (Confidential.dat). If the file is read-only, it unlocks the file so it can be edited. If it is not read-only, it sets the attribute to protect the file. This creates a “toggle” behavior.
Sample Code
using System;
using System.IO;
public class Program
{
public static void Main()
{
// Target file path
string filePath = "Confidential.dat";
// 1. Create the file if it does not exist for testing
if (!File.Exists(filePath))
{
File.WriteAllText(filePath, "TOP SECRET DATA");
}
// 2. Create a FileInfo instance
var fileInfo = new FileInfo(filePath);
Console.WriteLine($"File: {fileInfo.Name}");
// 3. Check the current attribute and branch
if (fileInfo.IsReadOnly)
{
Console.WriteLine("Status: [Locked] The file is read-only.");
// Change attribute: Remove read-only (make it writable)
fileInfo.IsReadOnly = false;
Console.WriteLine("Action: Unlocked. You can now edit or delete the file.");
}
else
{
Console.WriteLine("Status: [Editable] This is a normal file.");
// Change attribute: Set to read-only (prevent overwriting)
fileInfo.IsReadOnly = true;
Console.WriteLine("Action: Locked the file.");
}
// 4. Confirm the change
fileInfo.Refresh(); // Update the cache
Console.WriteLine($"Current attribute (IsReadOnly): {fileInfo.IsReadOnly}");
}
}
Execution Result (First Run: Starting from Normal State)
File: Confidential.dat
Status: [Editable] This is a normal file.
Action: Locked the file.
Current attribute (IsReadOnly): True
Execution Result (Second Run: Starting from Locked State)
File: Confidential.dat
Status: [Locked] The file is read-only.
Action: Unlocked. You can now edit or delete the file.
Current attribute (IsReadOnly): False
Explanation and Technical Points
1. The IsReadOnly Property
FileInfo.IsReadOnly is a convenient property that automatically determines if the FileAttributes.ReadOnly flag is included in the file’s attributes.
- Get: Returns
trueif the file is read-only. - Set: Assigning
trueapplies the read-only attribute in the OS, while assigningfalseremoves it.
2. Deleting Read-Only Files
If you try to use File.Delete() or overwrite a file using StreamWriter while it has the read-only attribute, an UnauthorizedAccessException will occur. When a program needs to delete or update a file, a safe design pattern is to check this attribute first and set IsReadOnly = false if necessary before proceeding.
3. Other Attributes (Hidden Files, etc.)
If you want to manipulate attributes other than read-only (such as Hidden or System files), you must use the File.GetAttributes and File.SetAttributes methods along with bitwise operations. However, for specifically handling the read-only status, using FileInfo is the most intuitive and simplest method.
