获取磁盘上文件的所有者 [英] Get owner of file on disk

查看:34
本文介绍了获取磁盘上文件的所有者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我当前的宏,它运行良好,但是我想在Cells(r,10)中添加文件所有者.这样的事情怎么实现?我找不到要使用的命令,例如File.Owner之类的东西.

Below is my current macro, which is working great, but I would like to add in Cells(r, 10) the owner of the file. How can such a thing be accomplished? I can't find the command to use like File.Owner or something.

Sub DoFolder(Folder)
If Ans = vbNo Then GoTo NoSub                                           'Switching according to ans.
Dim SubFolder
For Each SubFolder In Folder.SubFolders
    DoFolder SubFolder
Next
NoSub:
Dim File
For Each File In Folder.Files
    FName = File.Name
    If InStrRev(FName, ".") = 0 Then GoTo NextFile                      'If "." not found then go to next file
    Cells(r, 1) = Left(FName, InStrRev(FName, ".") - 1)                 'File Name
    ActiveSheet.Hyperlinks.Add Anchor:=Cells(r, 2), _
                               Address:=File.Path, _
                               TextToDisplay:=File.Path                 'Hyperlinking Path
    Cells(r, 3) = File.DateLastModified                                 'Date Last Modified
    Cells(r, 4) = Round(File.Size / 1024, 3)                            'in KBs Rounded to 3 Decimal places
    Cells(r, 5) = Right(FName, Len(FName) - InStrRev(FName, ".") + 1)   'File Extension
    r = r + 1
NextFile:
Next
End Sub

推荐答案

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

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

这篇关于获取磁盘上文件的所有者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆