Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set File access rule

Tags:

c#

.net

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

like image 535
User6996 Avatar asked Aug 31 '12 12:08

User6996


1 Answers

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.

like image 50
CSmith Avatar answered Sep 23 '22 13:09

CSmith