none
How can I change Readonly Attribute of a directory? RRS feed

Answers

  • Hope this helps you:
    http://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol.aspx


    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 6:06 AM
  • SetAccessControl is not needed unless you're wanting to set permissions.  ReadOnly doesn't apply to the directory itself, but rather to the files within the directory.  If you're wanting to set all the files in a directory to ReadOnly, you need to fetch a list of all the files, and then call File.SetAttribute.  Alternatively, you can get a list of FileInfo objects and set the Attributes property directly.  Here's the former option:

    foreach (string file in Directory.GetFiles(@"C:\subdirectory\", "*", SearchOption.AllDirectories))
        File.SetAttributes(file, FileAttributes.ReadOnly);


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 10:07 AM
    Moderator
  • To preserve other attributes, you want to call SetAttributes something like this

    File.SetAttributes(file, File.GetAttributes(file) | FileAttributes.ReadOnly);

    Mattias, C# MVP
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 11:19 AM

All replies

  • Hope this helps you:
    http://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol.aspx


    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 6:06 AM
  • SetAccessControl is not needed unless you're wanting to set permissions.  ReadOnly doesn't apply to the directory itself, but rather to the files within the directory.  If you're wanting to set all the files in a directory to ReadOnly, you need to fetch a list of all the files, and then call File.SetAttribute.  Alternatively, you can get a list of FileInfo objects and set the Attributes property directly.  Here's the former option:

    foreach (string file in Directory.GetFiles(@"C:\subdirectory\", "*", SearchOption.AllDirectories))
        File.SetAttributes(file, FileAttributes.ReadOnly);


    David Morton - http://blog.davemorton.net/ - @davidmmorton
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 10:07 AM
    Moderator
  • To preserve other attributes, you want to call SetAttributes something like this

    File.SetAttributes(file, File.GetAttributes(file) | FileAttributes.ReadOnly);

    Mattias, C# MVP
    • Marked as answer by Harry Zhu Monday, May 25, 2009 2:37 AM
    Wednesday, May 20, 2009 11:19 AM