How to Set Files as Read Only in .NET


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);

 

 

About author


User avatar

Waqas Anwar

Founder of EzzyLearning.com, Senior Consultant, Microsoft .NET and Web Technologies

Comments