Solved

Create directory and apply write permissions to it

Posted on 2009-05-08
2
493 Views
Last Modified: 2013-11-27
I want to create a folder in either My Documents or the Public\Users folders on XP and Vista.

I can create the folder using Directory.CreateDirectory without any problem. However, it defaults to ReadOnly.

I've tried using the DirectorySecurity overload but cannot get it to work.

When I try the AddDirectorySecurity method, I get teh error message "AddDirectorySecurity" is not declared.

If I change to DirectorySecurity (as suggested by Visual Studio) I am told it is " a type and cannot be used as an expression.

Can someone point me in the right direction please?

Many thanks
Imports System

Imports System.IO

Imports System.Security.AccessControl
 

Public Class MyClass

Try

    If Directory.Exists(fullPath.ToString()) = False Then

        Directory.CreateDirectory(fullPath.ToString())

        AddDirectorySecurity(fullPath.ToString()), "DOMAIN\MyUser", FileSystemRights.WriteData, AccessControlType.Allow)

    End If

Catch ex As Exception
 

End Try

...

End Class

Open in new window

0
Comment
Question by:jindalee
2 Comments
 
LVL 1

Accepted Solution

by:
Anthony408 earned 500 total points
ID: 24342327
Try this code that i found here:

http://www.practicalguy.com/fs-directory-permission-net-cs-vb.aspx

Hope this helps :D

-Anthony
' create directory inforamtion 

        Dim directoryInfo As New DirectoryInfo("c:\temp")

        ' get access control for directory 

        Dim directorySecurity As DirectorySecurity = directoryInfo.GetAccessControl()

        ' add directory permission for specific user account 

        directorySecurity.AddAccessRule(New FileSystemAccessRule("Administrator", FileSystemRights.FullControl, AccessControlType.Allow))

        ' set access control for directory 

        directoryInfo.SetAccessControl(directorySecurity)

Open in new window

0
 
LVL 1

Author Closing Comment

by:jindalee
ID: 31579702
Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

18 Experts available now in Live!

Get 1:1 Help Now