recursively change permissions of a directory

A

Aek

What is the best way to recursively change the permissions of the
directory we are installing into? Is there a nice way to do this in C#
..NET?

We are using an MSI installer and will need to add some custom actions
to change the permissions on the install directory and its sub
folders/files so that they can be accessed correctly via a network
share later on.

We would want to add Everyone full access to the install directory (its
a closed 3 node system running custom application on winXP pro, so
security isnt an issue)

I found a win32 C++ example but it was quite involved.

I would prefer some C# .NET.
The other alternative is to use cacls in a batch file but this is less
than ideal as we have less control over things when something goes
wrong for the user.. if we have C# code we can raise errors, log
things, popup nice informative error messages etc.

thanks

Josh
 
K

Kevin Spencer

Use the System.IO.DirectoryInfo and System.IO.FileInfo classes, in tandem
with the System.Security.AccessControl.DirectorySecurity and
System.Security.AccessControl.FileSecurity classes, in a recursive method.

What you do is to create a method to which you pass the path to a Directory.
The method gets a DirectoryInfo instance for that Directory, and uses that
to get an array of FileInfo instances, using the GetFiles method. It then
uses the DirectoryInfo.SetAccessControl method (to which an instance of the
DirectorySecurity is passed) to set the permissions for that Directory. For
each FileInfo, use the FileInfo.SetAccessControl method to set the
permissions for that file. The method then calls the
DirectoryInfo.GetDirectories method to get a list of DirectoryInfo instances
within that Directory, and calls itself for each DirectoryInfo in the list.

See
http://msdn2.microsoft.com/en-us/library/system.security.accesscontrol.directorysecurity.aspx
for documentation about the System.Security.AccessControl.DirectorySecurity
and FileSecurity classes.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Magician

A man, a plan, a canal.
a palindrome that has gone to s**t.
 

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