How can I set files read only?

J

jmDesktop

I know I can use FileAttributes.Normal but it doesn't always work. I
am using the FileSystemWatcher class. I have found that if I uncheck
the read-only flag from the directory and apply to all sub folders and
files the check will go away on the file. If I use the class it never
gets unset. I wanted to know if there was a way to activate the
uncheck apply to all programatically. thanks
 
J

Jeroen Mostert

jmDesktop said:
I know I can use FileAttributes.Normal but it doesn't always work. I
am using the FileSystemWatcher class. I have found that if I uncheck
the read-only flag from the directory and apply to all sub folders and
files the check will go away on the file. If I use the class it never
gets unset. I wanted to know if there was a way to activate the
uncheck apply to all programatically. thanks

FileSystemWatcher, as the name implies, watches. It can't be used to alter
file attributes, so I suspect you're confusing two things.

There is no built-in method for marking a directory and all subdirectories
and files as not read-only. You can manually walk the file system with (for
example) DirectoryInfo.GetFileSystemInfos() and reset the readonly attribute
yourself with .Attributes = .Attributes & ~FileAttributes.ReadOnly. Consult
the MSDN for more info on how to reset flags.

If, on the other hand, you just want to be informed when a file's attributes
change, then FileSystemWatcher could be used. You should set
..IncludeSubdirectories to true and .NotifyFilters to
NotifyFilters.Attributes to watch all files. Take note that this can
generate a large number of events.

If, on the gripping hand, you want to reset the readonly attribute of a file
you're watching with a FileSystemWatcher, first create a FileInfo instance
to it using the .FullName property of the FileSystemEventArgs you have, then
reset the readonly attribute in the manner described above.

If none of this should happen to apply, you'll have to be more specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top