Best way to make a file writeable in c#

will picture will · Jul 29, 2009 · Viewed 19k times · Source

I'm trying to set flag that causes the Read Only check box to appear when you right click \ Properties on a file.

Thanks!

Answer

Rex M picture Rex M · Jul 29, 2009

Two ways:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
fileInfo.IsReadOnly = true/false;

or

// Careful! This will clear other file flags e.g. `FileAttributes.Hidden`
File.SetAttributes(filePath, FileAttributes.ReadOnly/FileAttributes.Normal);

The IsReadOnly property on FileInfo essentially does the bit-flipping you would have to do manually in the second method.