且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取磁盘上文件的所有者

更新时间:2023-02-20 23:42:20

您可以通过安全实用程序"对象访问文件的安全属性(其中之一是所有者").

You can access the file's security properties (one of which is the Owner), through the Security Utility object.

Option Explicit

Sub test()
    Dim fName As String
    Dim fDir As String
    fName = "test.txt"
    fDir = "C:\Temp\"
    Debug.Print "The owner is " & GetFileOwner(fDir, fName)
End Sub

Function GetFileOwner(fileDir As String, fileName As String) As String
    Dim securityUtility As Object
    Dim securityDescriptor As Object
    Set securityUtility = CreateObject("ADsSecurityUtility")
    Set securityDescriptor = securityUtility.GetSecurityDescriptor(fileDir & fileName, 1, 1)
    GetFileOwner = securityDescriptor.owner
End Function