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/