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)
                                    bldr.Append("<tr><td>[DENY]</td>");


                                  if (rule.IsInherited)
                                    bldr.Append("<tr><td>[INHERITED]</td>");


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


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

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...