Tuesday, 22 January 2013

Get Read Only Files C#


This example enumerates Read Only files from a directory(optionally all sub directories) with specified search pattern(for ex: *,*.pdf,*.docx,*.xlsx)

        void GetReadOnlyFiles(String dir)
        {
        var query = System.IO.Directory.EnumerateFileSystemEntries(dir,
            "*.pdf",
            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);
                }
            }
        }

No comments:

Post a Comment