Vb.net folder permissions


From vb.net I'm trying to create a set of folders, with one folder having specific read/write permissions.
To create the folders, I'm currently using the following code :


Would there be a way for me to create the permissions while I'm creating the folder from vb.net?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Yes you can create the folder with certain rights.

For example, here I create a folder that has full control rights for all users using the well known sid for everyone (WorldSid).
Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Security.AccessControl
Imports System.Security.Principal

Module Module1
	Sub Main()
		Dim folder = "C:\testingFolder"
	End Sub
End Module

Module Extensions
	<Extension()> _
	Public Sub CreateDirectory(ByVal path As String)
			If Not Directory.Exists(path) Then
				Dim fEveryone = New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing)
				Dim fDirectorySecurity = New DirectorySecurity()
				Dim fFileSystemRights = FileSystemRights.FullControl
				Dim fInheritanceFlags = InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit
				Dim fPropagationFlags = PropagationFlags.None
				Dim fAccessControlType = AccessControlType.Allow
				Dim fDirectoryAccessRule = New FileSystemAccessRule(fEveryone, fFileSystemRights, fInheritanceFlags, fPropagationFlags, fAccessControlType)
				Directory.CreateDirectory(path, fDirectorySecurity)
			End If
		Catch ex As PathTooLongException
			Console.WriteLine("The path {0}; was too long.", path)
		Catch ex As UnauthorizedAccessException
			Console.WriteLine("The path {0}; cannot be created because you do not have the rights to create it.", path)
		Catch ex As Exception
			Console.WriteLine("Exception in {0} - {1}; {2}", ex.Source, ex, ex.Message)
		End Try
	End Sub
End Module

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.