Solved

Change folder security permissions in C sharp to everyone (full control)

Posted on 2007-04-01
14
1,205 Views
Last Modified: 2008-01-09
Hi, I am writing a program in C# that will create a directory under the ApplicationData folder and allow us to store our app files.  I need to be able to change the ntfs security permissions on this folder to allow "FULL CONTROL" to the "EVERYONE" group.  I was able to do this in C++ but I have no idea how to do it in C#.  Any sample code would be greatly appreciated!  Thanks.
0
Comment
Question by:farekat
  • 6
  • 5
  • 3
14 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 18835341
here is the code in VB.NEt

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 = "CALC.exe"
            p.StartInfo.Arguments = path & "/E /P " & user & ":" & rights
'or
'            p.StartInfo.Arguments = "C:\Inetpub\wwwroot\asp.net\mcad\statemanagement /E /P DAVE\IUSR_DAVE:F"

            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
------------------------------------------------

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

call:
SetPermissions("C:\inetpub\wwwroot\CardellReport", "everyone", "F")
==============================================================

0
 
LVL 12

Accepted Solution

by:
ShazbotOK earned 500 total points
ID: 18835809
Here this is probibly what you want:

        bool setAccessRules(ref System.IO.DirectoryInfo dInf)
        {
            try
            {
                System.Security.AccessControl addAccess = new System.Security.AccessControl.FileSystemAccessRule("EVERYONE",
                    System.Security.AccessControl.FileSystemRights.FullControl,
                    System.Security.AccessControl.AccessControlType.Allow);
                System.Security.AccessControl.DirectorySecurity dSec = new System.Security.AccessControl.DirectorySecurity();

                dSec.AddAccessRule(addAccess);

                dInf.SetAccessControl(dSec);
                //completed set of access control!
                return true;
            }
            catch (Exception e)
            {
                System.Diagnostics.Trace.Write(e.ToString());
                return false;
            }
        }
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18835822
whoops... typo...

Change This:   System.Security.AccessControl addAccess

To This: System.Security.AccessControl.AccessRule addAccess
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:farekat
ID: 18836842
thanks for your responses.  shazbotok, do i need to include any specific libraries to be able to use the function properly?  I'm sorry I'm new at this C# stuff
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18836885
the System.Security should be implicit - so no you should not have to add any references...  the Assembly used for these are System.IO and System.Security
0
 

Author Comment

by:farekat
ID: 18838416
I should also mention that Im using VS studio 2003 and .net 1.0...Im trying it and it does not seem to accept "Access Control"..again this may be a stupid question (can i download .net 2.0?)  is this where I can find use of  System.Security.AccessControl?  thanks :)
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18838446
Sorry, yes ... the AccessControl is new to the Framework in 2.0 and you would need to do so in VS.Net 2005 (or in a command line compiler - but that gets sticky)... or Express Edition

Downloading the 2.0 SDK can be found here:
http://www.microsoft.com/downloads/details.aspx?familyid=fe6f2099-b7b4-4f47-a244-c96d69c35dec&displaylang=en
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 18838449
You have to use .Net Framework 1.1 with VS2003
0
 

Author Comment

by:farekat
ID: 18847512
Okay I got VS2005 and .NET framework 2.0.  This might be a dumb question, could I do the function above if I passed a string?  Can I do a conversion from the string to the directoryinfo path (System.IO.DirectoryInfo)?  Can you help me out with that?
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 18849732
you can convert to string like that

Dim dInf As System.IO.DirectoryInfo
dInf = New System.IO.DirectoryInfo("c:\aa\")
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18849813
Yes my above function will allow you to do so - however in c# it would look like this (assuming you copy/paste OR emulate my example):

System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo("c:\\temp\\mypath\\");
if(setAccessRules(ref dInfo))
{
         MessageBox.Show("Directory permissions set...");
         // .....Do folder activity now
         // .... since dInfo is a "ref" you can reuse that object to access the folder
}
else
{
        MessageBox.Show("Set of permissions failed!!");
}
0
 

Author Comment

by:farekat
ID: 18851896
thank you for that I figured it out yesterday, do I need to specify the inheritflag so that the subfolder and files below have full control too?
0
 
LVL 12

Expert Comment

by:ShazbotOK
ID: 18851935
yes
0
 

Author Comment

by:farekat
ID: 18854101
It worked beautifully.  I was able to add the inherit flags.  Thank you ShazbotOK!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

810 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