Solved

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

Posted on 2007-04-01
14
1,217 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 40
c# DateTime Format validation 4 68
Consume a webservice via VB in Visual Studio 2015 3 20
Installing .NET 3.5 on Windows Server 2012 1 60
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

733 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