views:

208

answers:

0

Hi, I want to change a file in Windows (Vista) directory but as you may know, in Vista most of files in Windows directory have full permission on TrustedInstaller group and read and execute for Users and Administrators group.

My application is run as administrator but when I want to change the ownership of the file to the currently logged in user (which is an administrator) it gives me an unauthorized exception.

I have tried to add the user to the TrustedInstaller group but DirectoryEntry class can not find any group with this name.

Here is the code I used to change the ownership. The code works in XP.

WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();

FileSecurity acl = File.GetAccessControl(filename);
acl.SetOwner(currentUser.User);

File.SetAccessControl(filename, security);

and the code for finding the TrustedInstaller group is:

DirectoryEntry activeDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry groupEntry = activeDirectory.Children.Find("TrustedInstaller", "group");