How to Set Files as Read Only in .NET

Waqas Anwar
29 August 2012
4729 Views

If you want to change the state of the files as read only you can use one of the following techniques.

First of all import System.IO namespace
using System.IO;
Then either set IsReadOnly
property of FileInfo class to true or use SetAttributes function of File class as shown below:
var file = new FileInfo(@"D:\MyFolder\MyFile.txt");
file.IsReadOnly = true;

// or

File.SetAttributes(@"D:\MyFolder\MyFile.txt", FileAttributes.ReadOnly);