【问题标题】:C# get file owner in WindowsC#在Windows中获取文件所有者
【发布时间】:2015-10-22 20:58:36
【问题描述】:

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

File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount))

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

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

编辑: This link 解释发生了什么。它与管理员组中的用户创建的文件以及Windows如何处理这些文件的所有者有关。

【问题讨论】:

  • 如果做NTAccount ntAccount = File.GetAccessControl(filename).GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount)) as NTAccount;还是不正确
  • @3dd 仍然给我管理员,但 Chandrashekar Jupalli 发布的链接解释了原因。这与我的管理员权限以及 windows 如何处理管理员创建的文件有关
  • @Helic - 我现在尝试了您的代码,它总是为我尝试的所有文件(本地和网络驱动器上)返回正确的 NTAccount。即使我在管理员组...
  • @Herdo,真奇怪,让我用访客账号试试

标签: c# file owner


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2016-03-19
    • 2020-06-18
    • 1970-01-01
    • 2011-01-14
    • 2016-07-04
    • 2021-05-20
    相关资源
    最近更新 更多