|
-
July 22nd, 2009, 09:15 AM
#1
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
-
July 22nd, 2009, 09:35 AM
#2
Re: How to set the read-only property of a directory and its content programmatically
Have you tried the SetFileAttributes / SetFileAttributesEx APIs. ¿
-
July 22nd, 2009, 01:20 PM
#3
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?
-
July 22nd, 2009, 01:48 PM
#4
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
-
July 22nd, 2009, 02:37 PM
#5
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;
-
July 23rd, 2009, 01:01 AM
#6
Re: How to set the read-only property of a directory and its content programmatically
 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! 
Much easier!
-
July 23rd, 2009, 01:27 PM
#7
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
-
July 23rd, 2009, 02:40 PM
#8
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.
-
July 23rd, 2009, 07:16 PM
#9
Re: How to set the read-only property of a directory and its content programmatically
-
July 24th, 2009, 02:19 AM
#10
Re: How to set the read-only property of a directory and its content programmatically
 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
-
July 24th, 2009, 03:23 AM
#11
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 !
-
July 25th, 2009, 06:34 AM
#12
Re: How to set the read-only property of a directory and its content programmatically
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|