简体   繁体   English

Windows中的C#get file owner

[英]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. 但是,它给了我BUILTIN \\ Administrators作为所有者,但我可以在文件资源管理器中看到所有者是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. 它与管理员组中的用户创建的文件以及Windows如何处理这些文件的所有者有关。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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