Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to set Rights to Read only for all users except Administration. Following is my code but this throw an error as "Some or all identity references could not be translated." on 5th Line. Do i missing something ..Please help me

string dest = @"D:\Directory Test";
DirectoryInfo mydirInfo = new DirectoryInfo(dest);
DirectorySecurity mydirSec = mydirInfo.GetAccessControl();
string User = System.Environment.UserDomainName;
mydirSec.AddAccessRule(new FileSystemAccessRule(
                                    User, 
                                    FileSystemRights.Read, 
                                    InheritanceFlags.ObjectInherit, 
                                    PropagationFlags.InheritOnly, 
                                    AccessControlType.Allow));
mydirInfo.SetAccessControl(mydirSec);
Posted
Updated 6-Sep-14 22:54pm
v2
Comments
[no name] 5-Sep-14 6:04am    
how is it possible "only for all users except Administration." ?

Try to change the identity string to
C#
string User = System.Environment.UserName;

or
C#
string User = String.Format(@"{0}\{1}", System.Environment.UserDomainName, System.Environment.UserName);
 
Share this answer
 
v2
You may need to tweak exactly what you are doing with the permissions in order to remove existing permissions first, but the main problem is that your User information is rubbish - that's basically the PC name, rather than a user identifier.
Try this instead:
C#
string dest = @"D:\Directory Test";
DirectoryInfo mydirInfo = new DirectoryInfo(dest);
DirectorySecurity mydirSec = mydirInfo.GetAccessControl();
//string User = System.Environment.UserDomainName;
SecurityIdentifier User = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
mydirSec.AddAccessRule(new FileSystemAccessRule(
                                    User,
                                    FileSystemRights.Read,
                                    InheritanceFlags.ObjectInherit,
                                    PropagationFlags.InheritOnly,
                                    AccessControlType.Allow));
mydirInfo.SetAccessControl(mydirSec);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900