0 Replies - 228 Views - Last Post: 10 September 2009 - 04:08 PM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Set directory security settings

Posted 10 September 2009 - 04:08 PM

Description: Need reference to the following Namespace(s): System.IO System.Security.AccessControlSnippet for setting security settings for a specified directory
/// 
/// method for setting permissions for a specified directory
/// 
/// directory we're working with
/// user account
/// FileSystemRights enum value (http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights.aspx)
/// InheritanceFlags value (http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.inheritanceflags.aspx)
/// PropagationFlags value (http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.propagationflags.aspx)
/// AccessControlType value (http://msdn.microsoft.com/en-us/library/w4ds5h86.aspx)
/// 
public bool SetDirectorySecurity(string dir, string user, FileSystemRights rights, InheritanceFlags inheritance, PropagationFlags propagation, AccessControlType control)
{
    try
    {
        //create a new DirectoryInfo object for the directory we're working with
        DirectoryInfo dirInfo = new DirectoryInfo(dir);

        //get the current security settings for the specified directory
        DirectorySecurity security = dirInfo.GetAccessControl();

        //add the new access rule
        security.AddAccessRule(new FileSystemAccessRule(user, rights, inheritance, propagation, control));

        //apply the new settings
        dirInfo.SetAccessControl(security);

        //all went well
        return true;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return false;
    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1