Find the Owner of a folder

S

SimeonD

Hi
Is there a way I can fnd the Owner of a folder, using vb.net? I know how to
find the permissions, but I can't figure how to find the owner.
Thanks
SimeonD
 
A

Armin Zingler

SimeonD said:
Hi
Is there a way I can fnd the Owner of a folder, using vb.net? I know
how to find the permissions, but I can't figure how to find the
owner.
Thanks
SimeonD

1. Create a DirectoryInfo object
2. Call it's GetAccessControl method. It returns a DirectorySecurity
object.
3. Call the GetOwner method of the returned object. Something like this:

Dim sidType As Type =
GetType(System.Security.Principal.SecurityIdentifier)
Dim owner As System.Security.Principal.SecurityIdentifier

owner = DirectCast(DS.GetOwner(sidType),
Security.Principal.SecurityIdentifier)

(DS is the DirectorySecurity object)

4. Owner is the owner. :)


AZ
 
L

Linda Liu[MSFT]

Thanks AZ for your reply!

Hi Simeon,

As AZ has suggested, we can get the DirectorySecurity object by calling
DirectoryInfo.GetAccessControl method first and then get the owner by
calling the DirectorySecurity.GetOwner method passing NTAccount as the
parameter.

The following is a sample:

Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Principal

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim di As New DirectoryInfo("directoryname")
Dim ds As DirectorySecurity = di.GetAccessControl()
Dim owner As NTAccount = CType(ds.GetOwner(GetType(NTAccount)),
NTAccount)
End Sub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

kimiraikkonen

Thanks AZ for your reply!

Hi Simeon,

As AZ has suggested, we can get the DirectorySecurity object by calling
DirectoryInfo.GetAccessControl method first and then get the owner by
calling the DirectorySecurity.GetOwner method passing NTAccount as the
parameter.

The following is a sample:

Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Principal

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim di As New DirectoryInfo("directoryname")
Dim ds As DirectorySecurity = di.GetAccessControl()
Dim owner As NTAccount = CType(ds.GetOwner(GetType(NTAccount)),
NTAccount)
End Sub

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer tohttp://msdn.microsoft.com/subscriptions/managednewsgroups/default.asp...
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) athttp://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights..

Hi,
To make syntax shorter, "my" namespace can also be used:
Dim owner As NTAccount = _ My.Computer.FileSystem.GetDirectoryInfo("c:
\resized").GetAccessControl.GetOwner(GetType(NTAccount))

Thanks,

Onur Güzel
 
A

Armin Zingler

Hi,
To make syntax shorter, "my" namespace can also be used:
Dim owner As NTAccount = _ My.Computer.FileSystem.GetDirectoryInfo("c:
\resized").GetAccessControl.GetOwner(GetType(NTAccount))

==========

I think Linda Liu's example is exactly the same, but just split into
some lines to make the steps easier to understand.

The My namespaces is not required at all. At least in this case it's not
the direct way. The call is delegated to
Microsoft.VisualBasic.FileIO.FileSystem.GetDirectoryInfo which again
creates a Sytem.IO.DirectoryInfo object. So, I'd directly do the latter
and get rid of the superfluous My-garbage.


Armin
 
L

Linda Liu[MSFT]

Hi Simeon,

I am reviewing this post in the newsgroup and would like to know the status
of this issue.

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

NickR

I have a question - What I really need is to get the Owner as a string so I
can either display it in a label or write it to a textfile. But when I try
the "Cstr" function on the NTAccount data type, i get a popup telling me it
can't be converted.

Is there any way to get the owner of a folder written to a string?
 
K

kimiraikkonen

I have a question - What I really need is to get the Owner as a string so I
can either display it in a label or write it to a textfile. But when I try
the "Cstr" function on the NTAccount data type, i get a popup telling me it
can't be converted.

Is there any way to get the owner of a folder written to a string?

Just use "ToString" whenever you want to get owner as string:

Full edition:


Imports System.IO
Imports System.Security.AccessControl
Imports System.Security.Principal

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim di As New DirectoryInfo("directory_path")
Dim ds As DirectorySecurity = di.GetAccessControl()
Dim owner As NTAccount = CType(ds.GetOwner(GetType(NTAccount)),
NTAccount)
' Show owner in messagebox as string
MsgBox(owner.ToString)
' or show in label as string
label1.text = owner.ToString
End Sub
End Class


Thanks,

Onur Güzel
 
N

NickR

thanks, that function worked.
But, now i have another question if anyone cares to answer it.

I am running XP Pro (SP2) on my machine and our network is a Microsoft
Exchange Network.

Why is it that the owner for the folder using the code described above is
different than the one I get when I do a batch file using the dir command
with /q switch?

the dir /q command in DOS gives me the person who created it: DomainName\user

but he above code gives me BUILTIN\ADMINISTRATORS

Any thoughts? Is more information?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top