简体   繁体   中英

C# get file owner in Windows

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.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM