Solved

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

Posted on 2008-06-24
4
4,186 Views
Last Modified: 2013-11-26
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
Comment
Question by:soousky
4 Comments
 
LVL 11

Accepted Solution

by:
melmers earned 125 total points
ID: 21872083
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
 

Author Closing Comment

by:soousky
ID: 31470411
Thanks
0
 
LVL 3

Expert Comment

by:IUFITS
ID: 23983074
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
 

Expert Comment

by:coolneip
ID: 35436335
This worked great!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now