43

How can I programatically remove the readonly attribute from a directory in C#?

Sam
  • 7,252
  • 16
  • 46
  • 65
Red Swan
  • 15,157
  • 43
  • 156
  • 238
  • @Lalit What are you trying to achieve? – ACP Feb 23 '10 at 05:30
  • 1
    see I have read only folder on my some path. I want to copy some files in that folder. so that want to temporary make it writable means remove readonly , then copy files then again make that foleder as readonly. can u help me plaese? This is requirement. – Red Swan Feb 23 '10 at 05:34
  • 2
    On Windows, having the readonly attribute set on a directory is actually pretty meaningless. You can still delete, rename, etc. that directory. See https://support.microsoft.com/en-us/help/326549/you-cannot-view-or-change-the-read-only-or-the-system-attributes-of-folders-in-windows-server-2003,-in-windows-xp,-in-windows-vista-or-in-windows-7 for more info. – Alastair Maw Mar 09 '17 at 10:52

8 Answers8

96
var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 35
    &= --> Append so dont touch all the other attributes and ~ --> complement so do the opposite of readonly – MarkKGreenway Jan 03 '11 at 21:55
  • This may be the correct answer. However, as Alastair commented on the question, you should read https://support.microsoft.com/en-us/help/326549 before use. It may not do what you think it does. Notice that the "Read-only" option on the File Properties dialog box says, "(Only applies to files in folder)". – Wallace Kelly Oct 01 '18 at 14:09
  • Quick additional note: You need to Refresh() before you read/update the attributes because directory information is cached. See: https://learn.microsoft.com/en-us/dotnet/api/system.io.filesysteminfo.attributes#remarks – Ben Keene Apr 28 '21 at 15:07
9

Here's a good link to examples of modifying file attributes using c#

http://www.csharp-examples.net/file-attributes/

based on their example, you can remove the Read Only attribute like this (I haven't tested this):

File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • 1
    @Red Swan: I just tested this to add hidden attributes, and it works just fine with directories too. – Petrucio Jan 18 '13 at 02:41
  • 1
    This works for files and directories because a directory is like a special file with the directory attribute set (FileAttributes.Directory). – seveves Apr 04 '16 at 07:25
6

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!

Use &= ~FileAttributes.ReadOnly; to remove ReadOnly flag.

Use |= FileAttributes.ReadOnly; to apply ReadOnly flag.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Guakamole2
  • 61
  • 1
  • 1
2

If you're attempting to remove the attribute of a file in the file system, create an instance of the System.IO.FileInfo class and set the property IsReadOnly to false.

        FileInfo file = new FileInfo("c:\\microsoft.text");
        file.IsReadOnly = false;
cbkadel
  • 264
  • 2
  • 5
1

And the version with everything at once if something did not work

        this._path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "My App Settings");

        if (!Directory.Exists(this._path))
        {
            Directory.CreateDirectory(this._path);

            DirectoryInfo directoryInfo = new DirectoryInfo(this._path);
            directoryInfo.Attributes &= ~FileAttributes.ReadOnly;

            FileSystemInfo[] info = directoryInfo.GetFileSystemInfos("*", SearchOption.AllDirectories);
            for (int i = 0; i < info.Length; i++)
            {
                info[i].Attributes = FileAttributes.Normal;
            }
        }
Pomian
  • 11
  • 2
0

Setting Attributes to FileAttributes.Normal worked for me on both folders and files.

SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
  • 1
    The only concern I have with this approach is what if the folder (or file) has another attribute? If a folder is hidden and readonly, and you only want to make it no longer readonly, your method will make it no longer hidden as well. That might have unintended consequences. – Brian J Mar 07 '14 at 19:25
0
    public static void DeleteDirectory(string path)
    {
        var directory = new DirectoryInfo(path) 
        { Attributes =FileAttributes.Normal };
        foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
        {
            info.Attributes = FileAttributes.Normal;
        }
        directory.Delete(true);
    }
-1

Got it finally. ;)

class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo di = new DirectoryInfo("c:\\test");

        FileAttributes f = di.Attributes;

        Console.WriteLine("Directory c:\\test has attributes:");
        DecipherAttributes(f);

    }

    public static void DecipherAttributes(FileAttributes f)
    {
        // To set use File.SetAttributes

        File.SetAttributes(@"C:\test", FileAttributes.ReadOnly);

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");

        // To remove readonly use "-="
        f -= FileAttributes.ReadOnly;

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");
        else
            Console.WriteLine("Not ReadOnly");
    }
}
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146