Home
Manage Your Code
Snippet: Get folder security information (C#)
Title: Get folder security information Language: C#
Description: Get folder security information, show security tab infor by right clicking a folder. Views: 1535
Author: Jason Huo Date Added: 12/11/2008
Copy Code  
1public static DataTable GetFolderGroups(string folderPath)
2        {
3                FileSecurity fs = System.IO.File.GetAccessControl(folderPath);
4                AuthorizationRuleCollection arc = fs.GetAccessRules(true, true, typeof(NTAccount));
5
6                foreach (FileSystemAccessRule fsar in arc)
7                {
8
9                    //ignore everyone

10                    if (fsar.IdentityReference.Value.ToLower() == "everyone")
11                        continue;
12                    //ignore BUILTIN

13                    if (fsar.IdentityReference.Value.ToLower().StartsWith("builtin"))
14                        continue;
15                    if (fsar.IdentityReference.Value.ToUpper() == @"NT AUTHORITY\SYSTEM")
16                        continue;
17
18                    DataRow row = ACLTable.NewRow();
19
20                    string group = fsar.IdentityReference.Value;
21                    int nindex = group.IndexOf('\\');
22                    if (nindex > 0)
23                    {
24
25                        row["Identity"] = group.Substring(nindex + 1, group.Length - nindex - 1);
26                        //Debug.WriteLine(row["Identity"]);

27
28                        row["AcceptControlType"] = fsar.AccessControlType.ToString();
29                        //Debug.WriteLine(row["AcceptControlType"]);

30
31                        row["FileSystemRights"] = fsar.FileSystemRights.ToString();
32                        //Debug.WriteLine(row["FileSystemRights"]);

33
34                        ACLTable.Rows.Add(row);
35                    }
36
37                }
38
39                return ACLTable;
40    }