• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1244
  • Last Modified:

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

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
farekat
Asked:
farekat
  • 6
  • 5
  • 3
1 Solution
 
iboutchkineCommented:
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
 
ShazbotOKCommented:
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
 
ShazbotOKCommented:
whoops... typo...

Change This:   System.Security.AccessControl addAccess

To This: System.Security.AccessControl.AccessRule addAccess
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
farekatAuthor Commented:
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
 
ShazbotOKCommented:
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
 
farekatAuthor Commented:
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
 
ShazbotOKCommented:
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
 
iboutchkineCommented:
You have to use .Net Framework 1.1 with VS2003
0
 
farekatAuthor Commented:
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
 
iboutchkineCommented:
you can convert to string like that

Dim dInf As System.IO.DirectoryInfo
dInf = New System.IO.DirectoryInfo("c:\aa\")
0
 
ShazbotOKCommented:
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
 
farekatAuthor Commented:
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
 
ShazbotOKCommented:
yes
0
 
farekatAuthor Commented:
It worked beautifully.  I was able to add the inherit flags.  Thank you ShazbotOK!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now