简体   繁体   中英

apply permissions to the child folders using NTFS

I used the below code for applying permissions for a folder. That worked fine but I want the permissions to be applied for all the child folders also inside the main folder. How do I to achieve that?

        ADsSecurity objADsSec;
        SecurityDescriptor objSecDes;
        AccessControlList objDAcl;
        AccessControlEntry objAce1;
        AccessControlEntry objAce2;
        AccessControlEntry objAce3;
        Object objSIdHex;
        ADsSID objSId;

        objADsSec = new ADsSecurityClass();
        objSecDes = (SecurityDescriptor)(objADsSec.GetSecurityDescriptor("FILE://" + vPath));
        objDAcl = (AccessControlList)objSecDes.DiscretionaryAcl;

        objSId = new ADsSIDClass();
        objSId.SetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SAM,   UserName.ToString());
        objSIdHex = objSId.GetAs((int)ADSSECURITYLib.ADS_SID_FORMAT.ADS_SID_SDDL);


        objAce2 = new AccessControlEntryClass();
        objAce2.Trustee = (objSIdHex).ToString();
        objAce2.AccessMask = (int)ActiveDs.ADS_RIGHTS_ENUM.ADS_RIGHT_GENERIC_READ;
        objAce2.AceType = (int)ActiveDs.ADS_ACETYPE_ENUM.ADS_ACETYPE_ACCESS_ALLOWED;
        objAce2.AceFlags = (int)ActiveDs.ADS_ACEFLAG_ENUM.ADS_ACEFLAG_VALID_INHERIT_FLAGS;
        objDAcl.AddAce(objAce2);

Did you already try recursive searching for directories?

This code should work normally

void DirSearch(string sDir) 
{
    try 
    {
       foreach (string d in Directory.GetDirectories(sDir)) 
       {
        foreach (string f in Directory.GetFiles(d, txtFile.Text)) 
        {
           lstFilesFound.Items.Add(f);
        }
        DirSearch(d);
       }
    }
    catch (System.Exception excpt) 
    {
        Console.WriteLine(excpt.Message);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM