|
-
Sep 28th, 2004, 01:35 PM
#1
Thread Starter
Fanatic Member
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.
-
Sep 28th, 2004, 01:58 PM
#2
yay gay
Windows OS does not store such information
\m/  \m/
-
Sep 28th, 2004, 02:07 PM
#3
Addicted Member
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
-
Sep 28th, 2004, 02:10 PM
#4
yay gay
ah, I didnt know about that NFTS thing.
I dont know though, but if you ever happen to know post it here
\m/  \m/
-
Sep 28th, 2004, 03:28 PM
#5
Thread Starter
Fanatic Member
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.
-
Sep 29th, 2004, 07:07 AM
#6
Frenzied Member
Here's an example. Not very straightforward.
Tengo mas preguntas que contestas
-
Sep 29th, 2004, 10:52 AM
#7
Thread Starter
Fanatic Member
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.
-
Sep 29th, 2004, 11:02 AM
#8
Thread Starter
Fanatic Member
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.
-
Sep 29th, 2004, 11:29 AM
#9
Thread Starter
Fanatic Member
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.
-
Sep 30th, 2004, 03:24 AM
#10
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."
-
Sep 30th, 2004, 10:49 AM
#11
Thread Starter
Fanatic Member
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.
-
Sep 30th, 2004, 10:51 AM
#12
-
Sep 30th, 2004, 10:59 AM
#13
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|