C# Retrieve just the user accounts when displaying folder permissions and exclude system accounts

I have a windows desktop application that gets the file access rules for a particular folder and displays them in a list box. The code is below. It displays everything, including system accounts (NTAuthority\System, NTAuthority\Local Server) with the individual user accounts (abc\mike.smith, abc\jan.jones). I'd like to filter out the system accounts and display only the user accounts. Is there a way to do this? I'm using C#, but this is my attempt at working with Active Directory. Thanks for any input.

private void GetFolderPerms()
{
Directory.Security dSecurity = Diectory.getAccessControl(@"C:\MyFolder');
foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
     {
        lstPerms.Items.Add(rule.IdentityReference.value);
      }
}

Open in new window

dodgerfanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
Hi Dodgerfan,

The code you have provided is not right. I am not sure it is deliberate or someone is messing with you (if someone gave you that code - that is).

Anyways, to answer your query
Replace
{
        lstPerms.Items.Add(rule.IdentityReference.value);
      }

Open in new window

with
string ruleValue = rule.IdentityReference.Value;

                if (!ruleValue.StartsWith("BUILTIN\\") && !ruleValue.StartsWith("NT AUTHORITY\\"))
                {
                    lstPerms.Items.Add(rule.IdentityReference.Value);
                }

Open in new window


Regards,
Chinmay.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dodgerfanAuthor Commented:
Perfect, thank you!
0
Chinmay PatelChief Technical NinjaCommented:
Glad I could help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.