How to remove ReadOnly Attribute from a file?


Hello friends,

Here’s a nice little piece of code you can use to change file attributes of a particular file, its particularly useful when you’re trying to delete a read only file.

string savedCardFilePath = “My absolute file path”;

if (File.Exists(savedCardFilePath))
{
/*
* Remove readonly attribute off of the file if it exists before execution
*
**/
if(File.GetAttributes(savedCardFilePath) == FileAttributes.ReadOnly)
File.SetAttributes(savedCardFilePath, FileAttributes.Normal);
File.Delete(savedCardFilePath);
}

 

UPDATE May 14, 2012:

There have been some problems reported and better solutions given on this article so I updated the code to:

FileAttributes attributes = File.GetAttributes(file);

if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(file, attributes ^ FileAttributes.ReadOnly);

Thanks for the feedback and sorry about the mistake πŸ™‚

Β 

 

Advertisements

Author: Imran Akram

A .NET/SharePoint Consultant and a political enthusiast who believes in putting the feet of the elected and/or unelected officials to fire in order to get good governance.

17 thoughts on “How to remove ReadOnly Attribute from a file?”

  1. This will not work if more than one attribute is set on a file (ReadOnly and Archive, for example)

    if (File.Exists(savedCardFilePath))
    {
    FileAttributes fileAttributes = File.GetAttributes(savedCardFilePath);
    if ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    File.SetAttributes(savedCardFilePath, FileAttributes.Normal);
    }

    This should do it,
    Thanks for the help

  2. Actually, even the reposted version has a subtle bug. It will also remove the other attributes which is not expected behavior. It should be like this:

    var attr = File.GetAttributes(file).Dump();

    if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    File.SetAttributes(file, (attr ^ FileAttributes.ReadOnly));

    That will remove only the ReadOnly attribute and leave the others intact.

    1. John, good catch and nice use of XOR. However, what is Dump()?

      This works for me:

      FileAttributes attributes = File.GetAttributes(file);

      if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
      File.SetAttributes(file, attributes ^ FileAttributes.ReadOnly);

    1. does the user relevant your program have access to the file? As an easy way out, grant full access to user “Everyone” and see if it works, you should not do it with the production environment though!

      1. I’m working on managing some system files within my application,i know that if i take ownership of the folder it will solve the problem and your code will execute without a problem but i wish to take ownership/file programmatically

      2. hmm well then that’s really a separate story. And I think, in order to take the ownership programatically, the program itself has to be running under an Administrator account or some account that has the right to do this kind of thing, hear what I’m saying? You really need to run your app under a higher set of privileges.

  3. Can you *PLEASE* correct the original article????
    It’s totally wrong… and unless you read tons of “comments” you’ll never know it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s