title: How do I Copy a File and preserve ownership?
body: <p>I need to replace all the files in one directory with backup files in another directory.  ALL the file properties/permissions/ownership must be retained.  File.Copy, just like Windows Explorer, copies the files, clears all permissions, and changes the owner to myself.  </p>
  
  <p>I found an example on SO that SHOULD preserve its original permissions but doesn't: <a href="https://stackoverflow.com/questions/9163831/copy-a-file-with-its-original-permissions/9164000#9164000">Copy a file with its original permissions</a></p>
  
  <p>The code:</p>
  
  <pre><code>File.Copy(originFile, destinationFile);
  FileInfo originFileInfo = new FileInfo(originFile);
  FileInfo destinationFileInfo = new FileInfo(destinationFile);
  FileSecurity ac1 = originFileInfo.GetAccessControl(AccessControlSections.All);
  ac1.SetAccessRuleProtection(true, true);
  destinationFileInfo.SetAccessControl(ac1);
  </code></pre>
  
  <p>I get a PrivilegeNotHeldException:</p>
  
  <pre><code>The process does not possess the 'SeSecurityPrivilege' privilege which is required for this operation.
  </code></pre>
  
  <p>If I disable UAC I get this error instead:</p>
  
  <pre><code>The security identifier is not allowed to be the owner of this object.
  </code></pre>
  
  <p>I get this exception with AccessControlSections.All and AccessControlSections.Owner.  The code works if I change the enum to AccessControlSections.Access, but only the permissions are retained, not the ownership.  I am a local admin, and even when the destination is my local PC it doesn't work.  And I am running Visual Studio 2010 as administrator.</p>