Does anybody know how in .NET?
Thanks in advance :thumb:
Printable View
Does anybody know how in .NET?
Thanks in advance :thumb:
Windows OS does not store such information
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
ah, I didnt know about that NFTS thing.
I dont know though, but if you ever happen to know post it here :D
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 :mad:
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
Here's an example. Not very straightforward.
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:bigyello:
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.
Well this does the trick, it will find the owner of a file on a windows machine. :D
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
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
What do you mean by that? What API calls?
Samba API.
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.