Tuesday, May 17, 2011

Remove read only attribute of a file in C#

To manipulate file attribute in C# we can use the System.IO.FileInfo class.
So, let’s say that we have a “Read Only” file located at C:\temp\test.jpg what we need to modify; however, we need to remove the “Read Only” attribute before we can continue.

Here is how to do it:

  FileInfo file = new FileInfo(@"C:\temp\test.jpg");
  file.Attributes = file.Attributes & ~FileAttributes.ReadOnly;

The above sample uses the bitwise operator in order to remove the “Read Only” attribute while ensuring that all other attributes that the file may have are not also removed during this operation.

No comments:

Post a Comment