Reputation: 149
How can I verify in C# if a folder has set "Security" to "Everyone - Full Control: Allow?
Many Thanks!
Upvotes: 2
Views: 418
Reputation: 81493
It should be as easy as
var ctrl = new DirectoryInfo(@"D:\something").GetAccessControl();
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var accessRules = ctrl.GetAccessRules(true, true, typeof(SecurityIdentifier));
var result = accessRules
.Cast<FileSystemAccessRule>()
.Any(rule =>
rule.IdentityReference.Value == everyone.Value && // check everyone
rule.AccessControlType == AccessControlType.Allow && // check allow
rule.FileSystemRights == FileSystemRights.FullControl); // check full control
Note : This is windows only (obviously), and you might need System.IO.FileSystem.AccessControl
nuget
Additional Resources
Directory.GetAccessControl Method
Returns the Windows access control list (ACL) for a directory.
Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.
Indicates a SID that matches everyone.
DirectoryObjectSecurity.GetAccessRules(Boolean, Boolean, Type) Method
Gets a collection of the access rules associated with the specified security identifier.
Upvotes: 5