Get File Owner of a UNC file when run on a Windows 2000 Server
Posted on 2005-03-15
I have the need to be able to retrieve the file owner from a host of files that live throughout our network. We are provided the UNC names and need to return the file owner. I can get this working on my workstation, although its very slow, using a mapped drive but not with a unc name. As a last resort the mapped drive is OK. The problem is that I need to run this from a server (windows 2000 Server) and that even using a mapped drive, no owner is returned.
Question is 1) How can I get the file owner from a unc filename, and 2) how can I get it to run on the server?
The code is:
Public Function GetFileOwner(ByVal strFile As String) As String
Dim oManagementObject As New ManagementObject("Win32_LogicalFileSecuritySetting.path='" & strFile & "'")
Dim oManagementBaseObject As ManagementBaseObject
'oManagementObject.Path = ""
Dim oDescriptor As ManagementBaseObject
Dim oOwner As ManagementBaseObject
Dim Owner As PropertyDataCollection
Dim strOwner As String
'Get the security descriptor for this object
oManagementBaseObject = oManagementObject.InvokeMethod("GetSecurityDescriptor", Nothing, Nothing)
oDescriptor = oManagementBaseObject.Properties("Descriptor").Value()
'Get the Ownerobject
oOwner = oDescriptor.Properties("Owner").Value
'Read the Properties to a Propertydatacollection Object
Owner = oOwner.Properties
strOwner = Owner("Name").Value
Catch e As Exception
' Problem getting information
strOwner = ""