0

我使用以下代码为文件夹应用权限。这工作得很好,但我希望权限也适用于主文件夹内的所有子文件夹。我该如何实现呢?

        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);
4

1 回答 1

0

您是否已经尝试递归搜索目录?

此代码应该可以正常工作

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);
    }
}
于 2012-07-16T06:31:49.437 回答