Windows中的C#get file owner

Hel*_*lic 6 c# file owner

我想使用下面的代码获取文件的所有者

File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))
Run Code Online (Sandbox Code Playgroud)

但是,它给了我BUILTIN\Administrators作为所有者,但我可以在文件资源管理器中看到所有者是Domain\MyUserName.

为什么会发生这种情况以及如何解决?

编辑: 此链接说明发生了什么.它与管理员组中的用户创建的文件以及Windows如何处理这些文件的所有者有关.

小智 2

我能够通过这个获得文件的实际所有者...不确定这是否是您需要的。 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());