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
Re: How to set the read-only property of a directory and its content programmatically
Have you tried the SetFileAttributes / SetFileAttributesEx APIs. ¿ :)
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?
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
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;
Re: How to set the read-only property of a directory and its content programmatically
Quote:
Originally Posted by
Shuja Ali
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! :lol:
Much easier! :thumb:
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
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.
Re: How to set the read-only property of a directory and its content programmatically
Re: How to set the read-only property of a directory and its content programmatically
Quote:
Originally Posted by
Shuja Ali
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
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 !
Re: How to set the read-only property of a directory and its content programmatically
Quote:
Originally Posted by
nabeelisnabeel
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 :)