Tuesday, 22 January 2013

How to Change Read only Attribute C#

How to Change Read only Attribute C#


This example changes read only attributes for a specified files in the Directory Sepecified
optionally with searchString(for ex: only .docx files, or .xlxs files, or *.txt files)
and Top level directory or all Directories.

Example:

void ChangeReadOnlyAttribute(String dir)
{
var query = System.IO.Directory.EnumerateFileSystemEntries(dir,
    "*.txt",
    SearchOption.AllDirectories);
           
    foreach(String s in query)
    {
        System.IO.FileInfo fInfo = new FileInfo(s);
        FileAttributes fa=fInfo.Attributes;
        if (fa == (FileAttributes.ReadOnly))
        {
            Console.WriteLine(s);
            File.SetAttributes(s, FileAttributes.Normal);
        }
    }
}

Tags:Change Read only Attribute C#,Remove Read Only Attribute C#, Set File Attribute to Normal,