简体   繁体   中英

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?

在此处输入图像描述

Many Thanks!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM