Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am opening a folder using
OpenFileDialog
in vb.net

but before opening it, i want to check whether it has permission to open or not.

I am already checking whether folder exist or not using

System.IO.Directory.Exists(Directory)


Now if folder exists then i want to check whether it has permission to open or not.

What I have tried:

VB
Dim bDoesItExist As Boolean = System.IO.Directory.Exists(sDirectory)
Return bDoesItExist


to check folder exists or not
Posted
Updated 5-Dec-22 4:54am

Try this:
C#
DirectorySecurity ds = Directory.GetAccessControl(@"C:\Program Files");
foreach (FileSystemAccessRule rule in ds.GetAccessRules(true, true, typeof(NTAccount)))
    {
    Console.WriteLine($"Account:{rule.IdentityReference.Value}\n   {rule.FileSystemRights}:{rule.AccessControlType}");
    }
Or (auto translated to child code):
VB
Dim ds As DirectorySecurity = Directory.GetAccessControl("C:\Program Files")

For Each rule As FileSystemAccessRule In ds.GetAccessRules(True, True, GetType(NTAccount))
    Console.WriteLine($"Account:{rule.IdentityReference.Value}\n   {rule.FileSystemRights}:{rule.AccessControlType}")
Next
You should be able to work it out from there.
 
Share this answer
 
Comments
Maciej Los 24-Aug-21 6:35am    
5ed!
There's no other way ;) to check if a user has permissions to read/write in/to specific folder than to access it.

For example:
VB.NET
Dim myfolder As String = "C:\Windows\" 'must ends with "\"

'try to open folder
Try
	Process.Start("explorer.exe", myfolder) 'folder will be opened via explorer if user has got sufficient permissions
Catch Ex As Exception
	Console.WriteLine(Ex.Message)
End Try


More at: Try...Catch...Finally statement - Visual Basic | Microsoft Docs[^]
 
Share this answer
 
v3
i am using this code.

VB
Dim myinfo As New DirectoryInfo(sDirectory)
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
				 
	If (acc.AccessControlType = AccessControlType.Allow) Then
		Return True
	ElseIf (acc.AccessControlType = AccessControlType.Deny) Then
		Return False
	End If
Next



but need more testing
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900