C # get file owner in Windows

I want to get the owner of the 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 the owner of Domain \ MyUserName in the file explorer.

Why is this happening and how can this be fixed?

Edit: This link explains what is happening. This applies to files created by users in the Administrator group, and how Windows processes the owner of these files.

+6
source share
1 answer

I was able to get the actual owner of the file with 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());

+1
source

All Articles