Sunday, February 6, 2011

Enumerating NTFS permissions in C#.NET

Enumerating NTFS permissions in .NET is easy with the DirectorySecurity Classes and Security.AccessControl included in .NET 2.0. I incorporated it into my MobileExec 2011 program (screenshot to the left).  You will be able to tell which security permission is inherited, and what deny permissions are set. 

 FileSecurity sec = File.GetAccessControl(path);

if (sec == null)

  this.Label1.Style["text-align"] = "center";
  Label1.ForeColor = Color.Maroon;
  Label1.Text = "Path not found: <br /><b>" + path + "</b>";

                            Label1.Text += "<table>";
                            foreach(FileSystemAccessRule rule 
                                in sec.GetAccessRules(true, true, typeof(NTAccount)))
                                StringBuilder bldr = new StringBuilder();

                                  if (rule.AccessControlType == AccessControlType.Deny)

                                  if (rule.IsInherited)

                                  bldr.AppendFormat("{0} ", "<td><b>" + 
                                      rule.IdentityReference + "</b></td>");
                                  bldr.Append("<td>" + rule.FileSystemRights + "</td>");

                                Label1.Text += bldr.ToString() + "</tr>";

No comments:

Post a Comment