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 });
No comments:
Post a Comment