getting folder permissions in vb.net 2008

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi All

I need help finding which access permissions are set on a particular folder
in windows

I am using...

Dim myinfo As New DirectoryInfo(myfolder)
Dim dSecurity As Security.AccessControl.DirectorySecurity =
myinfo.GetAccessControl()
Dim fSecurity As Security.AccessControl.AuthorizationRuleCollection =
dSecurity.GetAccessRules(True, True,
Type.GetType("System.Security.Principal.NTAccount"))

For Each myacc As Security.AccessControl.AccessRule In fSecurity
Debug.WriteLine(myacc.IdentityReference.Value.ToString & " " &
myacc.AccessControlType)
Next

All I get is 0 for the myacc.AccessControlType even though in windows
explorer it shows permissions for numerous windows accounts


Regards
Steve
 
Hello Steve,

I made a little bit changes to your code and now it works.

Some points you might be interested in:

First, you need to cast AccessRule to FileSystemAccessRule, which contains
the detailed File System rights value.
The value contains flags indicating each individual right (combined by "Or"
operator).

Second, the AccessControlType property specifies whether an AccessRule
object is used to allow or deny access. It doesn't contain the actual
rights as you originally expected.

For more details about FileSystemAccessRule, please see:
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesy
stemaccessrule.aspx

And the FileSystemRights enum:
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesy
stemrights.aspx

And the AccessControlType enum:
http://msdn.microsoft.com/en-us/library/w4ds5h86.aspx

Finally, here is the updated code:

Dim myinfo As New DirectoryInfo(myfolder)
Dim dSecurity As DirectorySecurity = myinfo.GetAccessControl()
Dim fSecurity As AuthorizationRuleCollection = _
dSecurity.GetAccessRules(True, True,
Type.GetType("System.Security.Principal.NTAccount"))

For Each myacc As Security.AccessControl.AccessRule In fSecurity
Dim acc As FileSystemAccessRule = DirectCast(myacc,
FileSystemAccessRule)
Console.WriteLine(acc.IdentityReference.Value)
Console.Write("Access Control Type: ")
Console.WriteLine(IIf(acc.AccessControlType = AccessControlType.Allow,
"Allow", "Deny"))

If (acc.FileSystemRights And FileSystemRights.FullControl) =
FileSystemRights.FullControl Then
Console.Write("FullControl" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ReadData) =
FileSystemRights.ReadData Then
Console.Write("ReadData" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.WriteData) =
FileSystemRights.WriteData Then
Console.Write("WriteData" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ListDirectory) =
FileSystemRights.ListDirectory Then
Console.Write("ListDirectory" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ExecuteFile) =
FileSystemRights.ExecuteFile Then
Console.Write("ExecuteFile" & vbTab)
End If

Console.WriteLine()
Console.WriteLine()
Next

' ***** End Code *****

Please let me know how it works for you.

Best regards,

Jie Wang

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/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days 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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Jie

Worked a treat. Thanks for your assistance

Regards
Steve


"Jie Wang [MSFT]" said:
Hello Steve,

I made a little bit changes to your code and now it works.

Some points you might be interested in:

First, you need to cast AccessRule to FileSystemAccessRule, which contains
the detailed File System rights value.
The value contains flags indicating each individual right (combined by
"Or"
operator).

Second, the AccessControlType property specifies whether an AccessRule
object is used to allow or deny access. It doesn't contain the actual
rights as you originally expected.

For more details about FileSystemAccessRule, please see:
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesy
stemaccessrule.aspx

And the FileSystemRights enum:
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesy
stemrights.aspx

And the AccessControlType enum:
http://msdn.microsoft.com/en-us/library/w4ds5h86.aspx

Finally, here is the updated code:

Dim myinfo As New DirectoryInfo(myfolder)
Dim dSecurity As DirectorySecurity = myinfo.GetAccessControl()
Dim fSecurity As AuthorizationRuleCollection = _
dSecurity.GetAccessRules(True, True,
Type.GetType("System.Security.Principal.NTAccount"))

For Each myacc As Security.AccessControl.AccessRule In fSecurity
Dim acc As FileSystemAccessRule = DirectCast(myacc,
FileSystemAccessRule)
Console.WriteLine(acc.IdentityReference.Value)
Console.Write("Access Control Type: ")
Console.WriteLine(IIf(acc.AccessControlType = AccessControlType.Allow,
"Allow", "Deny"))

If (acc.FileSystemRights And FileSystemRights.FullControl) =
FileSystemRights.FullControl Then
Console.Write("FullControl" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ReadData) =
FileSystemRights.ReadData Then
Console.Write("ReadData" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.WriteData) =
FileSystemRights.WriteData Then
Console.Write("WriteData" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ListDirectory) =
FileSystemRights.ListDirectory Then
Console.Write("ListDirectory" & vbTab)
End If

If (acc.FileSystemRights And FileSystemRights.ExecuteFile) =
FileSystemRights.ExecuteFile Then
Console.Write("ExecuteFile" & vbTab)
End If

Console.WriteLine()
Console.WriteLine()
Next

' ***** End Code *****

Please let me know how it works for you.

Best regards,

Jie Wang

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/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days 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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Back
Top