File Owner

  • Thread starter Thread starter BHz00
  • Start date Start date
B

BHz00

I see that I can identify a files owner in windows explorer, hpw do I get
that information using VB.net
 
BHz00 said:
I see that I can identify a files owner in windows explorer, hpw do I get
that information using VB.net

\\\
Imports System.IO
Imports System.Security.Principal
....
Dim Owner As String = _
File.GetAccessControl(<path>).GetOwner(GetType(NTAccount)).Value
///
 
Herfried

I place your suggestion into my code, replace <path> with my variable, what
do I need to replace (NTAccount) with? The code stop with an
IdentityNotMappedException was unhandled. How or what do I need to do to
handle it?

BHz
 
BHz00 said:
I place your suggestion into my code, replace <path> with my variable,
what
do I need to replace (NTAccount) with? The code stop with an
IdentityNotMappedException was unhandled. How or what do I need to do to
handle it?

Well, I used 'NTAccount' and it returned my identity properly. I feel
sorry, but I am not sure what's causing the exception.
 
BHz00 said:
I place your suggestion into my code, replace <path> with my
variable, what do I need to replace (NTAccount) with? The code stop
with an IdentityNotMappedException was unhandled. How or what do I
need to do to handle it?

If you can't persuade it to work, then:

Imports System.Management

Function getOwner(ByVal path As String) As String
Dim mgmt As New
ManagementObject("Win32_LogicalFileSecuritySetting.path='" & path & "'")
Dim secDesc As ManagementBaseObject =
mgmt.InvokeMethod("GetSecurityDescriptor", Nothing, Nothing)
Dim descriptor As ManagementBaseObject =
CType(secDesc.Properties("Descriptor").Value, ManagementBaseObject)
Dim owner As ManagementBaseObject =
CType(descriptor.Properties("Owner").Value, ManagementBaseObject)
Return owner.Properties("Name").Value.ToString()
End Function

(watch for line-wrap)

Andrew
 
Andrew,

I must be dense this week. The Imports System.Management NameSpace gives me
an error message that it cannot be found. I have looked for it, but didn't
find anything.
 
Andrew,

I realized that I have to add a reference for system.management. Funny thing
is that Visual Studio crashes when I try to add the required dll. Any
ideas????
 
:
OK, I have added system.management. The function will run if I place a
string into the path as expected.

Function getOwner(ByVal path As String) As String
Dim mgmt As New
ManagementObject("Win32_LogicalFileSecuritySetting.path='" & path & "'")
Dim secDesc As ManagementBaseObject =
mgmt.InvokeMethod("GetSecurityDescriptor", Nothing, Nothing)
Dim descriptor As ManagementBaseObject =
CType(secDesc.Properties("Descriptor").Value, ManagementBaseObject)
Dim owner As ManagementBaseObject =
CType(descriptor.Properties("Owner").Value, ManagementBaseObject)
--> Return owner.Properties("Name").Value.ToString()
End Function

The function gives me a NullReferenceException was unhandled - Use the
"new" keyword to create an object instance when used in the following code:

If System.IO.Directory.Exists(Path2Scan) Then
Dim DIPath As New DirectoryInfo(Path2Scan)
Dim SFIPath As FileInfo() = DIPath.GetFiles
Dim SFISingle As FileInfo
Dim SFIName As String = ""
Dim Length As Integer

For Each SFISingle In SFIPath
Dim FileOwner As String
System.Windows.Forms.Application.DoEvents()
SFileCount = SFileCount + 1
SFIName = SFISingle.Name
SFilePath = SFISingle.FullName
FileOwner = getOwner(SFilePath)
LblGenInfo.Text = SFilePath
Length = Len(SFilePath)
STotalSize = STotalSize + SFISingle.Length
FileSize = SFISingle.Length
FileLastMod = SFISingle.LastWriteTime
FileDateCreated = SFISingle.CreationTime
Next
End If
 
BHz00 said:
The function gives me a NullReferenceException was unhandled - Use the
"new" keyword to create an object instance when used in the following
code:

[Always] use Option Strict On at the top of your code - it will alert you to
problem areas.

For example, your sTotalSize variable should be an Int64 ("Long").

Andrew
 
Andrew,
[Always] use Option Strict On at the top of your code - it will alert you
to problem areas.
I am glad you wrote [Always] as you did, for learning the syntax it is not
bad to set it off, when your class is then as you think it can be, you can
set it to On. (And for your final version you have of course it forever to
ON)

Just to let people not think why was this in past not done by default (you
can set it as default in your options to). You know this of course, however
for others who read your message.

Cor
 
Sorry about the wrap. I had the owner sort of working. I could get the owner
of a specific file, but not for every file in a directory. I then realized
that the files crashing the program give something that is not recognized as
a valid file owner. Is there a way figure this out before it causes the an
operating problem?
 
Back
Top