Everything in Particular

September 5, 2007

Clearing the read-only flag on a file in C#

Filed under: Development — omatase @ 4:58 pm

[code lang="c#"]
FileInfo myFile = new FileInfo("c:\\myfile.txt");

if ((myFile & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// remove the read only attribute
myFile.Attributes = (FileAttributes)(Convert.ToInt32(myFile.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly));
}
[/code]

6 Comments »

  1. myFile.Attributes -= FileAttributes.ReadOnly; works as well

    Comment by Scott — August 26, 2008 @ 8:24 am

  2. awesome input, thanks Scott!

    Comment by omatase — August 26, 2008 @ 10:37 am

  3. In case someone needs the VB.NET Code:

    Dim myFile As FileInfo = New FileInfo(“c:\myfile.txt”)

    If myFile.IsReadOnly ThenThen
    ‘ remove the read only attribute
    myFile.IsReadOnly = False
    End If

    Comment by Deniz — October 21, 2008 @ 6:33 am

  4. For changing the flag, this is a little prettier
    // flip the read only attribute
    myFile.Attributes ^= FileAttributes.ReadOnly;

    Comment by Mark Scott — November 7, 2008 @ 6:34 am

  5. Scott’s solution does not consistently work. I’m not sure why exactly, but when applied repeatedly to a file it turned read-only again.

    This works:

    FileInfo info = new FileInfo(file);
    info.Attributes &= ~FileAttributes.ReadOnly;

    Comment by ripper234 — March 16, 2009 @ 4:44 am

  6. The easiest solution is:

    myFile.IsReadOnly = false;

    …or, as ripper234 wrote:

    myFile.Attributes &= ~FileAttributes.ReadOnly;

    Neither of these methods require a pre-test as they ALWAYS remove read-only status.

    ————–
    Scott’s solution (using the -= assignment operator) is dangerous for two reasons:
    (1) It works ONLY IF the ReadOnly attribute is set, thus a test is required beforehand.
    (2) It is performing a subtract operation, which is not is not the best choice when working with binary flags. The subtract operation works if condition 1 (above) is true, but additional subtract operations will ALTER OTHER BITS in the FileAttributes field!

    To appropriately manipulate binary flags, you should only use bitwise (binary) operators:

    & (bitwise AND) – clears bits
    | (bitwise OR) – sets bits
    ^ (bitwise XOR) – flip-flops bits
    ~ (bitwise negation) – flip-flops all bits

    You can also use the corresponding compound assignment counterparts:

    &= (bitwise AND and assign)
    |= (bitwise OR and assign)
    ^= (bitwise XOR and assign)

    Not appropriate for flags, but appropriate for other binary operations:

    <> (shift right) – moves bits to the right (same as divide by n^2, but faster)

    ———–

    How ripper234’s method works:

    FileAttributes.ReadOnly = 0×0000000000000001

    In order to use the bitwise AND operator to clear a bit, we need the complement of this:

    ~FileAttributes.ReadOnly = 0×1111111111111110

    Next, we perform the bitwise AND, then assign it back:

    myFile.Attributes = myFile.Attributes & ~FileAttributes.ReadOnly;

    The &= operator is shorthand for the above and yields:

    myFire.Attributes &= ~FileAttributes.ReadOnly;

    Comment by Kevin P. Rice — August 25, 2009 @ 12:19 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress