I find there is very little code about this topic, so I research it and excute , below is the code, what I demonstrate is that the NTAccount must be correct, otherwise , you can't add the Accessrule into the DirectorySecurity.
string directoryPath = "c://documents";// directory path
// get the current identital accout logged on
string account = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
FileSystemAccessRule directorySystemAccessRule = new FileSystemAccessRule(
new NTAccount(account),
FileSystemRights.Write,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow);
directorySecurity.AddAccessRule(directorySystemAccessRule);
}
catch (Exception ex)
{
}