I have a little program that removing account permissions from shared folders. But on some folder's security tab there accounts like this "S-1-5-21-2008445439-890656017-1691616715-1589748". I have the permission to login that server and remove manually but whit my code I couldn't do because of this error below. How can I remove these accounts. Thanks.
private void button2_Click(object sender, EventArgs e)
{
var security = Directory.GetAccessControl(txtBoxPath.Text);
var rules = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAccessRule rule in rules)
{
if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
{
string name = rule.IdentityReference.Value;
RemoveFileSecurity(txtBoxPath.Text, name,
FileSystemRights.FullControl |
FileSystemRights.Modify |
FileSystemRights.Read |
FileSystemRights.ReadAndExecute |
FileSystemRights.ReadPermissions |
FileSystemRights.Synchronize |
FileSystemRights.ListDirectory |
FileSystemRights.ChangePermissions |
FileSystemRights.Delete,
AccessControlType.Allow);
MessageBox.Show("OK");
}
}
}
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
An unhandled exception of type 'System.Security.Principal.IdentityNotMappedException' occurred in mscorlib.dll
Additional information: Some or all identity references could not be translated.