Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-04-01
14
Medium Priority
?
1,237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Industry Leaders: 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!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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 recursion in the C programming language.

610 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