Link to home
Start Free TrialLog in
Avatar of tbaseflug
tbaseflugFlag for United States of America

asked on

Programtically Set Folder Security

Is there a way to programtically set permissions at the folder / directory level?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Avatar of tbaseflug

ASKER

TheLearnedOne -

Using the below, from the link - do I have to pass in any data such as the path:

    Public Shared Function SetPermissions(ByVal path As String, ByVal user As String, ByVal rights As String) As String
        Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
        Try
            p.StartInfo.FileName = "CACLS"
            p.StartInfo.Arguments = path & "/E /P " & user & ":" & rights
            p.StartInfo.RedirectStandardInput = True
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.UseShellExecute = False
            p.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Hidden

            p.Start()
            p.WaitForExit(1000)

            Return p.StandardOutput.ReadToEnd.ToString
        Catch ex As Exception
            Return ex.Message
        Finally
            If Not p Is Nothing Then
                p.Dispose()
                p = Nothing
            End If
        End Try
    End Function
I modified your comment to show the new function.

Explanation:

/E - Edit ACL instead of replacing it
/P - Replace specified user's access rights:  N=None, R=Read, W=Write, C=Change, F=Full

Bob
Bob -

Thanks!  So, the following should work, correct:

SetPermissions("C:\inetpub\wwwroot\CardellReport", "everyone", "F")
I would hope so :)

Bob
Avatar of ihenry
ihenry

I used code from this link to do the same thing, but it's written in c#.
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e6098575-dda0-48b8-9abf-e0705af065d9
 
and this guy managed to convert some of the code to vb.net, awesome!
credits to graye
https://www.experts-exchange.com/questions/21352828/How-to-Read-Set-Folder-Permissons-in-VB-NET.html
ihenry -

Would this work in a web form?
It would work to a certain stages. Try to copy and paste the code into another class and use it from your webform and make sure your web app security context has enough privilege to modify NTFS permission of the folder.

What I like most about the code is that it handles the "ACEs ordering" while cacls.exe utility in some cases fails to do so, which can causes some NTFS permission entries to be ineffective. Another problem with cacls.exe utility, it sucks at propagating inheritable ACEs and if you want to have that feature in your code, you might need to consider another utility like xcacls.exe as a replacement (unfortunately, the latter mentioned utility comes with another problems).
ihenry -

I will give it a try and let you know.


TheLearnedOne -

I have been trying to set it from the example provided and for some reason it just seems to ignore it - do you know if I have toput the entire path folder in question, or just the folder name itself?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial