Taking ownership of a file or folder Taking ownership of a file or folder windows windows

Taking ownership of a file or folder


I had the same problem and just posting here for anybody else who may come here searching like me:

You need to explicitly enable SeTakeOwnershipPrivilege in code as Luke mentions above. I found this Process Privileges to be really helpful dealing with this sort of thing.

Here is how it fixed my code:

using System;using System.Diagnostics;// ...using (new ProcessPrivileges.PrivilegeEnabler(Process.GetCurrentProcess(), Privilege.TakeOwnership)){    directoryInfo = new DirectoryInfo(path);    directorySecurity = directoryInfo.GetAccessControl();    directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);    Directory.SetAccessControl(path, directorySecurity);    }


Did you elevate your process via UAC first? On Windows 7, without UAC escalation, your process is running with the lower privileged token.