pepe
pepe

Reputation: 149

How can I verify in C# if a folder has "Security" to "Everyone - Full Control: Allow"?

How can I verify in C# if a folder has set "Security" to "Everyone - Full Control: Allow?

enter image description here

Many Thanks!

Upvotes: 2

Views: 418

Answers (1)

TheGeneral
TheGeneral

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.

SecurityIdentifier Class

Represents a security identifier (SID) and provides marshaling and comparison operations for SIDs.

WellKnownSidType Enum

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

Related Questions