【问题标题】:How to get the owner of the file in the windows 7?如何在 windows 7 中获取文件的所有者?
【发布时间】:2017-05-07 22:21:07
【问题描述】:

我有一个问题 - 在打开文件时的窗口中显示最后修改文件的人的姓名。如果您右键单击文件并选择属性和选项卡详细信息,则此信息可用...我看到所有者行和名称,但我不知道如何从我的脚本中获取此信息。

让我们查看文件中的属性:

\\server\project\sequences\ttt_sRnd.v016.mb

我使用 Python2.7,但我没有找到如何获取数据的解决方案……在 linux 中它可以工作。但不是在 Windows 中。 我试图控制台实用程序窗口。

dir /Q - 它适用于本地文件

C:\temp>dir /Q file.ext
11/06/2004  15:33           290,304 COMP\user       file.ext
               1 File(s)        290,304 bytes
               0 Dir(s)  316,720,226,304 bytes free

但在服务器上文件时不起作用:

\\server\project\sequences\>dir /Q file.ext
21/12/2016  16:00            66,372 ...                    file.ext
               1 File(s)         66,372 bytes
               0 Dir(s)  52,561,190,912 bytes free

这很奇怪,因为在资源管理器中我可以看到数据并且它们是可用的

好吧,试试另一个实用程序subinacl.exe

同样 - 处理本地文件而不处理服务器上的文件:

C:\temp>subinacl.exe /file file.ext /display=owner
/owner             =comp\user

C:\temp>subinacl.exe /file \\server\project\sequences\file.ext  /display=owner
\\server\project\sequences\file.ext - CreateFile Error : 1314 A required privilege is not held by the client.

我尝试takeown 和所有相同的 - 仅适用于本地文件:

C:\temp>takeown /F file.ext
SUCCESS: The file (or folder): "C:\temp\file.ext" now owned by user "COMP\user".

\\server\project\sequences\>takeown /F file.ext
ERROR: Access is denied.

它可能在 Windows 中有其他实用程序? 我什至准备自己编写这样的工具并从 python 调用它。但我不知道如何获得这些信息?告诉我如何在任何编程语言中崩溃问题?我相信在 C/С++ 或 C# 代码中,输出到控制台的 5 行代码的问题......如果是这样 - 很乐意提供帮助,然后我将从 python 中使用这个实用程序

【问题讨论】:

  • standard lookup(现在已经无耻地从 Tim 的网站上抄袭了两次,没有注明出处)将是 sd = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION);sid = sd.GetSecurityDescriptorOwner()。然后调用win32security.LookupAccountSid(machine, sid) 将 SID 映射到域和名称。
  • 从已删除的答案中,我们知道最后一部分以ERROR_TRUSTED_RELATIONSHIP_FAILURE (1789) 失败,因为您传递了None 作为机器名称并在域控制器中遇到了一些配置问题。尝试为机器名称传递“服务器”以直接查询它。
  • @eryksun 被删除的答案又出现了,
  • @AriGold,显然你看不到它,但是其他人抄袭了蒂姆的网站,然后在你添加你的答案之前删除了那个答案。当您基本上从某人的网站复制和粘贴时,您需要引用来源,否则人们会否决您。
  • @eryksun 成功了!谢谢! )

标签: python c windows file owner


【解决方案1】:

python 2.7

尝试使用 win32security 库中的函数(GetFileSecurity 和 LookupAccountSid),您将获得有关所有者的信息

import win32security

def GetOwner(filename):
    f = win32security.GetFileSecurity(filename, win32security.OWNER_SECURITY_INFORMATION)
    (username, domain, sid_name_use) =  win32security.LookupAccountSid(None, f.GetSecurityDescriptorOwner())
    return username

print GetOwner(r"\\some_shared_location\somefile.txt")

【讨论】:

  • 可以确认这个函数在 Python 3.6 中也可以工作!
猜你喜欢
  • 2020-06-18
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 2013-08-29
相关资源
最近更新 更多