6

I want to get the owner of a file using the code below

File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))

However, it gives me BUILTIN\Administrators as the owner, but I can see in the file explorer the owner is Domain\MyUserName.

Why this happens and how can fix it?

Edit: This link explain what happen. It is to do with the files created by users in the Administrator Group and how Windows handle the owner of these files.

Community
  • 1
  • 1
Helic
  • 907
  • 1
  • 10
  • 25
  • 1
    http://stackoverflow.com/questions/3370146/how-can-i-find-out-who-created-a-file-in-windows-using-net – Chandrashekar Jupalli Jul 31 '15 at 09:03
  • Is it still incorrect if you do `NTAccount ntAccount = File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount)) as NTAccount;` – 3dd Jul 31 '15 at 09:04
  • 1
    @3dd still gives me Administrators, but the link posted by Chandrashekar Jupalli explains why. It is to do with my Admin privilege and how windows handles file created by Adminstors – Helic Jul 31 '15 at 09:10
  • @Helic - I tried your code right now, and it always returns the correct `NTAccount` for all files I tried (locally and on network drives). Even though I'm in the admin group... – Stefan Over Jul 31 '15 at 09:13
  • @Herdo, that is strange, let me try it with a guest account – Helic Jul 31 '15 at 09:14
  • @Helic Sure. Maybe there's a different handling for the "local administrator group" and the "ADS administrator group". Also there might be differences in OS and ADS settings, that cause this issue for you. Obviously in my company network, my PC running Windows 8.0, it works correctly. – Stefan Over Jul 31 '15 at 09:22

1 Answers1

2

I was able to get the actual owner of a file by this... not sure if this is what you need or not. System.IO.FileInfo Fi = new System.IO.FileInfo(@"path2file");

MessageBox.Show(Fi.GetAccessControl().GetOwner(typeof(System.Security.Principal.SecurityIdentifier)).Translate(typeof(System.Security.Principal.NTAccount)).ToString());

Dustin
  • 63
  • 7