How can I programatically remove the readonly attribute from a directory in C#?
-
@Lalit What are you trying to achieve? – ACP Feb 23 '10 at 05:30
-
1see 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
-
2On 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 Answers
var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;

- 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
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);

- 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
-
1This 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
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.

- 4,532
- 4
- 53
- 64

- 61
- 1
- 1
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;

- 264
- 2
- 5
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;
}
}

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

- 35,571
- 4
- 52
- 61
-
1The 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
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);
}
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");
}
}

- 35,497
- 30
- 100
- 146
-
we want to remove the readonly flag from it. not just showing whether it is a readonly file/folder or not! – ebrahim.mr Jun 07 '21 at 06:33