Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to set full controls for folder win 7 by c#. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 22:03pm    
What does it mean, please?
—SA

1 solution

Hi Tuan,


Codeproject article : Adding File Access Permissions using DirectoryServices

Code given below sets a folder's and all child folders/files permission for a specific user to "Full Control".

C#
string Folder = "C:\TestFolder";
string Account = "a_user_account";
DirectorySecurity dSecurity = Directory.GetAccessControl(Folder,
                       AccessControlSections.All);
dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                       FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit|InheritanceFlags.ObjectInherit,
                       PropagationFlags.None,
                       AccessControlType.Allow));
Directory.SetAccessControl(folder, dSecurity);



C# - Set Folder Permission


C#
1 ) Set Access Control
DirectoryInfo dInfo = new DirectoryInfo(fileName);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule("everyone",FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,PropagationFlags.InheritOnly,AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);


2) Sharing foldel
ManagementClass mc = new ManagementClass("win32_share");
ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams("Description") = "My Shared Folder";
inParams("Name") = "Shared Folder Name";
inParams("Path") = "C:\\Folder1";
inParams("Type") = ShareResourceType.DiskDrive;
inParams("MaximumAllowed") = null;
inParams("Password") = null;
inParams("Access") = null; // Make Everyone has full control access.
ManagementBaseObject outParams = classObj.InvokeMethod("Create", inParams, null);


3) Only in Windows 7 and Vista, upgrade "Everyone" sharing right
//user selection
NTAccount ntAccount = new NTAccount("Everyone");

//SID
SecurityIdentifier userSID = (SecurityIdentifier)ntAccount.Translate(typeof(SecurityIdentifier));
byte[] utenteSIDArray = new byte[userSID.BinaryLength];
userSID.GetBinaryForm(utenteSIDArray, 0);

//Trustee
ManagementObject userTrustee = new ManagementClass(new ManagementPath("Win32_Trustee"), null);
userTrustee["Name"] = "Everyone";
userTrustee["SID"] = utenteSIDArray;

//ACE
ManagementObject userACE = new ManagementClass(new ManagementPath("Win32_Ace"), null);
userACE["AccessMask"] = 2032127;                                 //Full access
userACE["AceFlags"] = AceFlags.ObjectInherit | AceFlags.ContainerInherit;
userACE["AceType"] = AceType.AccessAllowed;
userACE["Trustee"] = userTrustee;

ManagementObject userSecurityDescriptor = new ManagementClass(new ManagementPath("Win32_SecurityDescriptor"), null);
userSecurityDescriptor["ControlFlags"] = 4; //SE_DACL_PRESENT
userSecurityDescriptor["DACL"] = new object[] { userACE };

//UPGRADE SECURITY PERMISSION
ManagementClass mc = new ManagementClass("Win32_Share");
ManagementObject share = new ManagementObject(mc.Path + ".Name='" + CondivisionName + "'");
share.InvokeMethod("SetShareInfo", new object[] { Int32.MaxValue, description, userSecurityDescriptor });
 
Share this answer
 
Comments
Tuấn Ngọc 26-Mar-13 10:04am    
hic hic have you a demo ? i don't this
paresh5lp 1-Oct-14 8:00am    
Thank You. .Its working very well !!!

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