CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2009
    Posts
    73

    Question How to set the read-only property of a directory and its content programmatically?

    Hi,

    Files and \irectories in Windows (XP) have the property "Read Only".
    How can I, programmatically, set the "Read Only" property of a certain directory so it will apply to all its content (files and subdirectories) ?

    The FileInfo class has a "ReadOnly" property, so it's not a problem, we can go over all the files in the directory recursivelt and set their ReadOnly property to false.
    But directories don't have this property. Instead they have the "Attributes" property, which can be assigned with FileAttributes.ReadOnly, but I tried several things and it didn't actually worked.

    A solution which doesn't include enumerating all the files and sub directories in the directory will be preferred, but any other solution will be OK too.

    Regards

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How to set the read-only property of a directory and its content programmatically

    Have you tried the SetFileAttributes / SetFileAttributesEx APIs. ¿

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to set the read-only property of a directory and its content programmatically

    Won't the DirectoryInfo class let you add read-only attributes to all sub-directories and files? Have you tried that?

  4. #4
    Join Date
    Apr 2009
    Posts
    73

    Question Re: How to set the read-only property of a directory and its content programmatically

    Hi,

    I tried several things with DirectoryInfo and they didn't work.

    Which code line are you suggesting?

    Regards,
    Lior

  5. #5
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to set the read-only property of a directory and its content programmatically

    I just tried DirectoryInfo class like this and it did setup all sub-folders with read-only attribute.
    Code:
                DirectoryInfo DirInfo = new DirectoryInfo(@"C:\Program Locations");
                DirInfo.Attributes = FileAttributes.ReadOnly;

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How to set the read-only property of a directory and its content programmatically

    Quote Originally Posted by Shuja Ali View Post
    I just tried DirectoryInfo class like this and it did setup all sub-folders with read-only attribute.
    Code:
                DirectoryInfo DirInfo = new DirectoryInfo(@"C:\Program Locations");
                DirInfo.Attributes = FileAttributes.ReadOnly;
    I missed that one!
    Much easier!

  7. #7
    Join Date
    Apr 2009
    Posts
    73

    Re: How to set the read-only property of a directory and its content programmatically

    Hi,

    First, as far as I know, the second code line override the provios value of the "Attributes" property (notice that the FileAttributes enum is a flag enum, and by assigning ReadOnly I'm positive that you are not preserving the rest of the properties of the Directory.

    Second, I tried it but the GUI still indicated that the directory is not read only.

    Regards

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to set the read-only property of a directory and its content programmatically

    Well that was just an illustration to show how to do it. You can always do something like this
    Code:
    DirInfo.Attributes |= FileAttributes.ReadOnly;
    That would make sure that all other attributes are saved.

    I tested it several times and it does work.

  9. #9
    Join Date
    Apr 2009
    Posts
    73

    Re: How to set the read-only property of a directory and its content programmatically

    Thanks!

  10. #10
    Join Date
    Apr 2006
    Posts
    220

    Re: How to set the read-only property of a directory and its content programmatically

    Quote Originally Posted by Shuja Ali View Post
    Well that was just an illustration to show how to do it. You can always do something like this
    Code:
    DirInfo.Attributes |= FileAttributes.ReadOnly;
    That would make sure that all other attributes are saved.

    I tested it several times and it does work.
    How bitwise OR operator will accomplish the given task, can you explain a bit

  11. #11
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: How to set the read-only property of a directory and its content programmatically

    OR operator will set the Attributes bit in DirInfo to READ_ONLY , thats how it works !

  12. #12
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to set the read-only property of a directory and its content programmatically

    Quote Originally Posted by nabeelisnabeel View Post
    How bitwise OR operator will accomplish the given task, can you explain a bit
    A file attribute is nothing but a binary value and if you want add to the exiting binary value, you will use a Bitwise OR operation. remember what we studied a decade back (as far as i remember), the bitwise operations are still helpful and very much in use everywhere

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured