How can i programatically remove the readonly attribute of folder from c# code ?
A:
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
2010-02-23 05:32:48
come on this all about File. wana abt Directory.... please.
Lalit
2010-02-23 05:42:22
A:
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
2010-02-23 05:36:19
A:
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");
}
}
TheMachineCharmer
2010-02-23 05:49:00
+5
A:
var di = new DirectoryInfo("SomeFolder");
di.Attributes &= ~FileAttributes.ReadOnly;
Darin Dimitrov
2010-02-23 07:59:35