0

Below is the code how to get the name of the file's owner:

using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;

//...

FileInfo fileInfo = new FileInfo(@"1.txt");
FileSecurity fileSecurity = fileInfo.GetAccessControl();
IdentityReference identityReference = fileSecurity.GetOwner(typeof(NTAccount));
MessageBox.Show(identityReference.Value);

Is it possible to write this data to the file using c#?

Amazing User
  • 3,473
  • 10
  • 36
  • 75

1 Answers1

0

From this question

File.Copy(@"1.txt", @"2.txt", true);
string userName = System.IO.File.GetAccessControl(@"1.txt").GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
var fs = File.GetAccessControl(@"2.txt");
var ntAccount = new NTAccount("DOMAIN", userName);
fs.SetOWner(ntAccount);

try {
   File.SetAccessControl(@"2.txt", fs);
} catch (InvalidOperationException ex) {
   Console.WriteLine("You cannot assign ownership to that user." +
    "Either you don't have TakeOwnership permissions, or it is not your user account."
   );
   throw;
}
Community
  • 1
  • 1
teo van kot
  • 12,350
  • 10
  • 38
  • 70