Link to home
Start Free TrialLog in
Avatar of CMS-Team
CMS-Team

asked on

Setting read/write/modify rights to everyone in a folder in C#

I'm copying a complete website into a specific location and I'm triying to give rights to everyone(LOCAL) to every folder in it because the user can download and make the server create files and folders, I'm using Win32_UserAccount and Win32_Account but for most users I'm getting the following error(Including aspnet which I think is the most important):

System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.
   at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess)
   at System.Security.Principal.NTAccount.Translate(Type targetType)
   at System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified)
   at System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule)
   at System.Security.AccessControl.FileSystemSecurity.AddAccessRule(FileSystemAccessRule rule)
   at


Check out my code below for more information.


SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain<>'"
                                     + System.Environment.UserDomainName.ToString() + "'");
                ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);
                foreach (ManagementObject mObject in mSearcher.Get())
                {
                        DirectorySecurity myDirectorySecurity = folder.GetAccessControl();
                        string User = System.Environment.UserDomainName + "\\" + mObject["Name"].ToString();
                                                myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.CreateFiles, AccessControlType.Allow));
                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Modify, AccessControlType.Allow));
                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Traverse, AccessControlType.Allow));
                        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.ChangePermissions, AccessControlType.Allow));
                        //myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Modify, AccessControlType.Allow));
                        folder.SetAccessControl(myDirectorySecurity);
                                    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of the_crazed
the_crazed
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial