Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Programtically Set Folder Security

Posted on 2005-05-09
10
Medium Priority
?
220 Views
Last Modified: 2010-04-23
Is there a way to programtically set permissions at the folder / directory level?
0
Comment
Question by:tbaseflug
  • 4
  • 4
  • 2
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961718
0
 

Author Comment

by:tbaseflug
ID: 13961757
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13961814
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:tbaseflug
ID: 13962038
Bob -

Thanks!  So, the following should work, correct:

SetPermissions("C:\inetpub\wwwroot\CardellReport", "everyone", "F")
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13962121
I would hope so :)

Bob
0
 
LVL 20

Expert Comment

by:ihenry
ID: 13966427
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
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21352828.html
0
 

Author Comment

by:tbaseflug
ID: 13967461
ihenry -

Would this work in a web form?
0
 
LVL 20

Expert Comment

by:ihenry
ID: 13967882
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).
0
 

Author Comment

by:tbaseflug
ID: 13968317
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?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13969447
I use this code that I got from a previous question, and I should give ihenry the credit for it (sometimes I forget where it comes from).

I use this code in my WinForms, and, as an administrator I have rights, so I wouldn't know of using it in any other environment, such as a web page.

I would imagine that you need the whole path.

In addition, you can redirect StandardError, and read if there are any errors from the StandardError:

p.StartInfo.RedirectStandardError = True

Dim errorMessage As String = p.StandardError.ReadToEnd()

Bob

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

569 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