3

I've been trying to find a way to store permissions on a directory. I am currently using SetACL to help. If I could change ownership on directories/registry without it that would be preferred, but that's a different story. An example is this:

Check current owner and permissions for that owner. Change to permissions and owner on button click > change it back to the original when another button is clicked.

To save the question of why, my team does technical support. We remote into a computer, perform our t/s and leave. We need to be able to change permissions then revert it back the way it was.

Any help is appreciated.

1 Answers1

4

If you use the System.Management and System.Management.Instrumentation namespaces, you can use the Directory.GetAccessControl method to get who has access to a folder. You can then use the AddAccessRule and SetAccessControl methods to actually apply your new permissions. When you are done, you could remove your new permissions. Here is an article that walks you through how to change permissions on a folder:

http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/

The basic code this author uses is as follows:

DirectoryInfo myDirectoryInfo = new DirectoryInfo(yourFolderHere);

// Get a DirectorySecurity object that represents the 
// current security settings.
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

// Builds a new user string for who we want to give permissions to
string User = System.Environment.UserDomainName + "\\" + yourUserName; 

// Creates the permissions to apply 
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, 
                                  FileSystemRights.FullControl, AccessControlType.Allow));

// Set the new access settings. 
myDirectoryInfo.SetAccessControl(myDirectorySecurity);

// Showing a Success Message
MessageBox.Show("Permissions Altered Successfully");
}

As for setting the owner, here is a good SO article on how to do so:

C# - How to use DirectorySecurity.SetOwner() ? I'm having troubles

However, if you wanted to set the owner to be someone else (since I would assume you don't need to assign ownership permissions to a user when they already have access to assign ownership permissions), this above method won't work I don't believe. I have found documentation on how to set the owner to be someone other than yourself. Here is the link:

http://blog.salamandersoft.co.uk/index.php/2009/10/setting-the-owner-of-files-and-directories-in-c/

Community
  • 1
  • 1
IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
  • BiggsTRC, I will test out what you just posted. Thank you for providing so much information! Project that is due Monday morning so thanks again for getting back so quickly – John Andrews May 21 '11 at 23:13
  • @John Andrews - Glad to be of help. Go ahead and give a shout out if you need help or get stuck somewhere. I want to see you succeed. – IAmTimCorey May 21 '11 at 23:16
  • Ok, I've got it so that I can change the Owner and set permissions. Two things: 1 when I set the permissions for an account, it just adds another in with "Special Permissions" and 2 I still can't store the permissions. Tried and tried but it's not working. – John Andrews May 22 '11 at 05:24
  • Here's what I've got so far...still don't have a way to save it, but if inheritance is set then I get a user full permissions, remove then permissions and reset inheritance. Work around for storing I guess. – John Andrews May 22 '11 at 07:40
  • @John Andrews - Here is a link with an example that might help you. It looks like their examples do exactly what you want to do: http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx – IAmTimCorey May 23 '11 at 06:09