Find File Owner Using .NET 2.0
-
I'm trying to find file/folder owners using VB 2005 and .NET 2.0. I've been successful using the System.Management namespace and WMI calls, but this takes a lot more coding than it looks like it would take using .NET 2.0. I've found the following C++ example at http://msdn.microsoft.com/msdnmag/issues/06/00/SecurityBriefs: void printTheOwnerOfThisFile(string path) { FileSecurity s = File.GetAccessControl(path); NTAccount user = (NTAccount)s.GetOwner(typeof(NTAccount)); Console.WriteLine(user); } ... printTheOwnerOfThisFile(@"c:\autoexec.bat"); Here's my attempt at translating it to VB 2005: Imports System.IO Imports System.Security.AccessControl Imports System.Security.Principal Module Module1 Sub Main() Dim FilePath As String = "c:\autoexec.bat" Dim s As FileSecurity = File.GetAccessControl(FilePath) Dim user As NTAccount = s.GetOwner(NTAccount) Console.WriteLine(user) For i As Int64 = 1 To 100000000000 i = i + 1 Next End Sub End Module (The For-Next loop is just to keep the console window open long enough to see what was written.) I'm getting a Build error on the GetOwner parameter in parentheses that "NTAccount is a type and cannot be used as an expression." However, the GetOwner method parameter is supposed to be "targetType as System.Type" according to MSDN. Can anyone tell me what I'm doing wrong? Is it just not possible to get the file owner in VB using .NET 2.0? Thanks in Advance
-
I'm trying to find file/folder owners using VB 2005 and .NET 2.0. I've been successful using the System.Management namespace and WMI calls, but this takes a lot more coding than it looks like it would take using .NET 2.0. I've found the following C++ example at http://msdn.microsoft.com/msdnmag/issues/06/00/SecurityBriefs: void printTheOwnerOfThisFile(string path) { FileSecurity s = File.GetAccessControl(path); NTAccount user = (NTAccount)s.GetOwner(typeof(NTAccount)); Console.WriteLine(user); } ... printTheOwnerOfThisFile(@"c:\autoexec.bat"); Here's my attempt at translating it to VB 2005: Imports System.IO Imports System.Security.AccessControl Imports System.Security.Principal Module Module1 Sub Main() Dim FilePath As String = "c:\autoexec.bat" Dim s As FileSecurity = File.GetAccessControl(FilePath) Dim user As NTAccount = s.GetOwner(NTAccount) Console.WriteLine(user) For i As Int64 = 1 To 100000000000 i = i + 1 Next End Sub End Module (The For-Next loop is just to keep the console window open long enough to see what was written.) I'm getting a Build error on the GetOwner parameter in parentheses that "NTAccount is a type and cannot be used as an expression." However, the GetOwner method parameter is supposed to be "targetType as System.Type" according to MSDN. Can anyone tell me what I'm doing wrong? Is it just not possible to get the file owner in VB using .NET 2.0? Thanks in Advance
is251rd wrote:
Dim user As NTAccount = s.GetOwner(NTAccount)
Change that to
Dim user As NTAccount = s.GetOwner(GetType(NTAccount))
is251rd wrote:
The For-Next loop is just to keep the console window open long enough to see what was written.
Have you tried
Console.ReadLine()
? That'll keep the window open until you hit enter :) Cheers, Will H -
is251rd wrote:
Dim user As NTAccount = s.GetOwner(NTAccount)
Change that to
Dim user As NTAccount = s.GetOwner(GetType(NTAccount))
is251rd wrote:
The For-Next loop is just to keep the console window open long enough to see what was written.
Have you tried
Console.ReadLine()
? That'll keep the window open until you hit enter :) Cheers, Will H