Solved

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

Posted on 2007-04-01
14
1,179 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now