Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Find File Owner Using .NET 2.0

Find File Owner Using .NET 2.0

Scheduled Pinned Locked Moved Visual Basic
csharpc++comsecurityhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    is251rd
    wrote on last edited by
    #1

    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

    K 1 Reply Last reply
    0
    • I is251rd

      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

      K Offline
      K Offline
      kasik
      wrote on last edited by
      #2

      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

      I 1 Reply Last reply
      0
      • K kasik

        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

        I Offline
        I Offline
        is251rd
        wrote on last edited by
        #3

        Thanks a bunch, Will! That works great! I've been combing the internet for a couple days trying to figure this out.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups