Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create directory and apply write permissions to it

Posted on 2009-05-08
2
Medium Priority
?
516 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Accepted Solution

by:
Anthony408 earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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