The follow function works on windows XP, now im trying it with windows 7 it returns IdentityNotMappedException error what is wrong? i have also change the application requestedexecutionlevel to admin.
private static void file_accessdeny(string fileName)
{
try
{
System.Security.AccessControl.FileSecurity accessdeny = System.IO.File.GetAccessControl(fileName);
accessdeny.SetAccessRule(new System.Security.AccessControl.FileSystemAccessRule("Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Deny));
System.IO.File.SetAccessControl(fileName, accessdeny);
}
catch (System.Exception E)
{
Console.WriteLine(E.Message);
System.Windows.Forms.MessageBox.Show(E.Message, "access deny");
}
}
Error: System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated
Try this instead in your code:
accessdeny.SetAccessRule(
new System.Security.AccessControl.FileSystemAccessRule(
new SecurityIdentifier(WellKnownSidType.WorldSid, null),
System.Security.AccessControl.FileSystemRights.FullControl,
System.Security.AccessControl.AccessControlType.Deny));
The error message says "could not be translated" - this is Windows telling you that when it tried to find a SID for the "Everyone" group (i.e. translate)...it couldn't find it by that name.
One reason for that is when you are running Windows under a different locale. For instance in German the group is called "Jeder" instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With