I have to assign permission on a folder and it's child folder and files programmatically using C#.NET. I'm doing this as below:
var rootDic = @"C:\ROOT";
var identity = "NETWORK SERVICE"; //The name of a user account.
try
{
var accessRule = new FileSystemAccessRule(identity,
fileSystemRights: FileSystemRights.Modify,
inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
propagationFlags: PropagationFlags.InheritOnly,
type: AccessControlType.Allow);
var directoryInfo = new DirectoryInfo(rootDic);
// Get a DirectorySecurity object that represents the current security settings.
DirectorySecurity dSecurity = directoryInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(accessRule);
// Set the new access settings.
directoryInfo.SetAccessControl(dSecurity);
}
catch (Exception ex)
{
//...
}
It does assign permission on my 'C:\ROOT' folder. But it assign permission to the Subfolders and Files only but not the 'ROOT' folder.
Q: How can I define the FileSystemAccessRule
instance to assign permission to the ROOT folder, Subfolders and files?
You just need to remove the PropagationFlags.InheritOnly
flag. By specifying that you are stating that the ACE should not apply to the the target folder. Use PropagationFlags.None
instead. You may find this MSDN article helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With