0

The following code does not work in .net 6 class library, because File.GetAccessControl method is not available in .net 6.

I need a function that will retrieve all the domain user groups that have read access to a file.

public List<string> GetFileReadGroups(string filePath)
        {
            var fileSecurity = File.GetAccessControl(filePath);
            var readRules = fileSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier));
            var groups = new List<string>();
            foreach (FileSystemAccessRule rule in readRules)
            {
                if ((rule.FileSystemRights & FileSystemRights.ReadData) == FileSystemRights.ReadData)
                {
                    if (rule.IdentityReference is SecurityIdentifier sid)
                    {
                        var account = (NTAccount)sid.Translate(typeof(NTAccount));
                        groups.Add(account.Value);
                    }
                }
            }
            return groups;
        }
Internet Engineer
  • 2,514
  • 8
  • 41
  • 54

0 Answers0