C# • Show a File’s DACL Entries

Listing 1. The file’s method loops through the access control entries that are associated with the file, and then displays the ACEs on the screen with the help of the custom ShowRightsForm dialog box.

private void ShowFileRights(string filename)
{
	FileSecurity fileSec =
		System.IO.File.GetAccessControl(filename);
	System.Type wantedType = typeof(
		System.Security.Principal.NTAccount);
	if (showFileSIDsCheckBox.Checked) {
		wantedType = typeof(
	System.Security.Principal.SecurityIdentifier);
	}
	AuthorizationRuleCollection rules =
		fileSec.GetAccessRules(true,
		showInheritedFileRightsCheckBox.Checked,
		wantedType);
	// loop through all the rules the object has
	List dataList =
		new List(10);
	foreach (FileSystemAccessRule rule in rules)
	{
		RuleData data = new RuleData();
		data.Type =
			rule.AccessControlType.ToString();
		data.Name = rule.IdentityReference.Value;
		data.Rights =
			rule.FileSystemRights.ToString();
		dataList.Add(data);
	}
	ShowRightsForm srf = new ShowRightsForm();
	string sddl =
		fileSec.GetSecurityDescriptorSddlForm(
		AccessControlSections.All);
	srf.ShowRuleData(filename, sddl, dataList);
}