Results 1 to 13 of 13

Thread: Get File Owner Name {SORT OF RESOLVED}

  1. #1

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537

    Question Get File Owner Name {SORT OF RESOLVED}

    Does anybody know how in .NET?

    Thanks in advance
    Last edited by cpatzer; Sep 29th, 2004 at 12:05 PM.
    In life you can be sure of only two things... death and taxes. I'll take death.

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Windows OS does not store such information
    \m/\m/

  3. #3
    Addicted Member corwin_ranger's Avatar
    Join Date
    Sep 2004
    Location
    CT
    Posts
    198
    Windows NT, 2000, XP & 2003 absolutely do store this information if you are using the NTFS file system.

    I don't know as yet how to access it from .NET as yet, however.

    Steve

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ah, I didnt know about that NFTS thing.

    I dont know though, but if you ever happen to know post it here
    \m/\m/

  5. #5

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    If you go to any folder in your OS and View->Details, then right click on any column header you can select owner.

    I don't know why Microsoft had to make this so hard to do programmatically


    I did however find this little code snippet in VB6 which I translated. Unfortunately parts of it don't work.

    Any suggestions?

    Code:
       Private Const OWNER_SECURITY_INFORMATION = &H1
        Private Const ERROR_INSUFFICIENT_BUFFER = 122&
        Private Const MAX_PATH = 255
    
    
        Private Declare Function GetFileSecurity Lib "advapi32.dll" _
           Alias "GetFileSecurityA" ( _
           ByVal lpFileName As String, _
           ByVal RequestedInformation As Long, _
        ByVal pSecurityDescriptor As Byte, _
           ByVal nLength As Long, _
        ByVal lpnLengthNeeded As Long) As Long
    
        Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll" _
       (ByVal pSecurityDescriptor As Object, _
        ByVal pOwner As Long, _
        ByVal lpbOwnerDefaulted As Long) As Long
    
        Private Declare Function LookupAccountSid Lib "advapi32.dll" _
           Alias "LookupAccountSidA" ( _
           ByVal lpSystemName As String, _
           ByVal Sid As Long, _
           ByVal Name As String, _
        ByVal cbName As Long, _
           ByVal ReferencedDomainName As String, _
        ByVal cbReferencedDomainName As Long, _
        ByVal peUse As Long) As Long
    
        Private Declare Function GetWindowsDirectory Lib "kernel32" _
           Alias "GetWindowsDirectoryA" ( _
           ByVal lpBuffer As String, _
           ByVal nSize As Long) As Long
    
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            Dim szfilename As String   ' File name to retrieve the owner for
            Dim bSuccess As Long       ' Status variable
            Dim sizeSD As Long         ' Buffer size to store Owner's SID
            Dim pOwner As Long         ' Pointer to the Owner's SID
            Dim Name As String         ' Name of the file owner
            Dim domain_name As String  ' Name of the first domain for the owner
            Dim name_len As Long       ' Required length for the owner name
            Dim domain_len As Long     ' Required length for the domain name
            Dim sdBuf() As Byte        ' Buffer for Security Descriptor
            Dim nLength As Long        ' Length of the Windows Directory
            Dim deUse As Long          ' Pointer to a SID_NAME_USE enumerated
            ' type indicating the type of the account
    
            ' Initialize some required variables.
    
            bSuccess = 0
            Name = ""
            domain_name = ""
            name_len = 0
            domain_len = 0
            pOwner = 0
            szfilename = Space(MAX_PATH)
    
            ' Obtain the path to the Windows Directory and use the Winnt256.bmp file
            ' as it should be on the system.
    
            szfilename = "C:\t.tif"
    
            bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0, 0&, sizeSD)
    
            If (bSuccess = 0) And _
               (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER) Then
                MsgBox("GetLastError returned  : " & Err.LastDllError)
            End If
    
            ' Create a buffer of the required size and call GetFileSecurity again.
    
            ReDim sdBuf(sizeSD)
            '!!!SIZE SD IS ZERO WHY???
    
    
            ' Fill the buffer with the security descriptor of the object specified
            ' by the szfilename parameter. The calling process must have the right
            ' to view the specified aspects of the object's security status.
    
            bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, sdBuf(0), sizeSD, sizeSD)
    
            If (bSuccess <> 0) Then
    
                ' Obtain the owner's SID from the Security Descriptor.
                '
                bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
                If (bSuccess = 0) Then
                    MsgBox("GetLastError returned : " & Err.LastDllError)
                End If
    
                ' Retrieve the name of the account and the name of the first
                ' domain on which this SID is found.  Passes in the Owner's SID
                ' obtained previously.  Call LookupAccountSid twice, the first time
                ' to obtain the required size of the owner and domain names.
    
                bSuccess = LookupAccountSid(vbNullString, pOwner, Name, name_len, _
                   domain_name, domain_len, deUse)
                If (bSuccess = 0) And _
                   (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER) Then
                    MsgBox("GetLastError returned : " & Err.LastDllError)
                End If
    
                '  Allocate the required space in the name and domain_name string
                '  variables. Allocate 1 byte less to avoid the appended NULL character.
                '
                Name = Space(name_len - 1)
                domain_name = Space(domain_len - 1)
    
                '  Call LookupAccountSid again to actually fill in the name of the owner
                '  and the first domain.
                '
                bSuccess = LookupAccountSid(vbNullString, pOwner, Name, name_len, _
                   domain_name, domain_len, deUse)
                If bSuccess = 0 Or Err.LastDllError <> 0 Then
                    MsgBox("GetLastError returned : " & Err.LastDllError)
                End If
    
                MsgBox("The Owner of " & szfilename & " is " & Name)
    
            End If
        End Sub
    In life you can be sure of only two things... death and taxes. I'll take death.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Here's an example. Not very straightforward.
    Tengo mas preguntas que contestas

  7. #7

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    I think that example will work well... If I could get this part to work

    Imports System.Management

    Error: Namespace or type "Management" for the Imports System.Management could not be found

    Does that one line work in your dev env?

    Thanks
    In life you can be sure of only two things... death and taxes. I'll take death.

  8. #8

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    Nevermind. I had to add a reference to C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Management.dll

    Will keep you posted on progress.

    Thanks again.
    In life you can be sure of only two things... death and taxes. I'll take death.

  9. #9

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    Well this does the trick, it will find the owner of a file on a windows machine.

    I COULD DO THE JIG

    Thanks to everybody for their help especially salvelinus.

    Does anybody know how to find the owner of a file on a Linux network share?


    Code:
    Imports System
    Imports System.Management
    
    
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Name = "Form1"
            Me.Text = "Form1"
    
        End Sub
    
    #End Region
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            QueryFileSecurity("c:\\t.tif")
            'This next one won't work however returns ""
            'H:\\Publishing\\2004-09-28\\Writer\\myFile.incd
    
        End Sub
    
    
    
    
    
    
    
        Public Sub QueryFileSecurity(ByVal fileName As String)
            Dim o As ManagementObject = New ManagementObject("Win32_LogicalFileSecuritySetting.Path=""" + fileName + """")
            Dim outP As ManagementBaseObject = o.InvokeMethod("GetSecurityDescriptor", Nothing, Nothing)
            If System.Convert.ToSingle(outP.Properties("ReturnValue").Value) = 0 Then
                Dim Descriptor As ManagementBaseObject = outP.Properties("Descriptor").Value
                Dim OwnerObject As ManagementBaseObject = Descriptor.Properties("Owner").Value
                Dim Owner As PropertyDataCollection = OwnerObject.Properties
                Dim OwnerText As String = Owner("Name").Value
                MsgBox(OwnerText)
            End If
        End Sub
    
    
    
    
    End Class
    In life you can be sure of only two things... death and taxes. I'll take death.

  10. #10
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi
    I'm not sure i think its impossible to obtain that information from Samba.

    Maybe using the API calls instead of WMI might work.

    regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  11. #11

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    What do you mean by that? What API calls?
    In life you can be sure of only two things... death and taxes. I'll take death.

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Samba API.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13

    Thread Starter
    Fanatic Member cpatzer's Avatar
    Join Date
    Sep 2004
    Posts
    537
    Let me be a little more specific.

    This code is going to reside on Win2K boxes running Version 1.1 .NET runtime. Accessing files on a Samba share. I have some experience with UNIX/Linux. I am however unfamiliar with Linux's underbelly.
    In life you can be sure of only two things... death and taxes. I'll take death.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width