问题描述
我想使用下面的代码获取文件的所有者
I want to get the owner of a file using the code below
File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))
但是,它给了我BUILTIN \ Administrators作为所有者,但是我可以在文件资源管理器中看到所有者是Domain \ MyUserName.
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?
:说明发生了什么.这与管理员组中用户创建的文件以及Windows如何处理这些文件的所有者有关.
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.
推荐答案
通过此操作,我能够获得文件的实际所有者...不确定这是否是您所需要的. System.IO.FileInfo Fi =新的System.IO.FileInfo(@"path2file");
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());
这篇关于C#在Windows中获取文件所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!