Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4223
  • Last Modified:

How to set/change folder permission in VB .NET 3.5

Hi,

I am trying to change a folder (and all its sub folders and files) permission in Visual studio 2008 and VB .Net 3.5. Also I can not use windows AD as it is not implemented.

What is the best way to do this?

I have looked around and there is some information about setting file/folder permissions s but its all older stuff and I hear there is new way to do this in VS 2008. But I can not find it.

What I want to do it change a folder (and all files and sub files) permission to full access do stuff and then change it to read only and execute.

I am doing this using Visual Basic and .NET 3.5.

Thanks


0
soousky
Asked:
soousky
1 Solution
 
melmersCommented:
Hi,

you can do this with .NET 2.0 functions.
Here an example to set the folder Permissions

I hope this helps

 
 'Needed imports
 Imports System.Security.AccessControl
 Imports System.IO
 
 'Code snippet
 Dim CurrentPath as String = "C:\TestPath"
 Dim UserName as String = "DOMAIN\MyUser" 
 
 Dim dInfo As New DirectoryInfo(CurrentPath)
 Dim dSecInfo As DirectorySecurity = dInfo.GetAccessControl(AccessControlSections.Access)
 Dim myRuleValue As Integer = 0
 
 myRuleValue =  FileSystemRights.FullControl   'Add your wanted Access here
 Dim myRule as FileSystemAccessRule
 myRule = New FileSystemAccessRule(UserName myRuleValue, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow)
 dSecInfo.AddAccessRule(myRule)
 dSecInfo.SetAccessRule(myRule)
 dInfo.SetAccessControl(dSecInfo)

Open in new window

0
 
soouskyAuthor Commented:
Thanks
0
 
IUFITSCommented:
Good code, just one comment, this line needs to have a comma after UserName:
myRule = New FileSystemAccessRule(UserName myRuleValue, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow)
dSecInfo.AddAccessRule(myRule)
dSecInfo.SetAccessRule(myRule)
dInfo.SetAccessControl(dSecInfo)
Open in New Window Select All
0
 
coolneipCommented:
This worked great!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now