应该是这个方法吧 MSDN没例子 Directory.CreateDirectory(String, DirectorySecurity)
谁说下后面那个参数如何设置谢谢我想创建一个文件夹 设置属性为禁止读取 写入

解决方案 »

  1.   

    是设置NTFS文件系统的文件夹权限吧, 参考下面的C代码, 使用P/invoke调用API实现
    void SetFilePermission(LPCTSTR FileName) 

     PSID pEveryoneSID = NULL; 
     BOOL a= TRUE;
    BOOL b= TRUE;
    BOOL c= TRUE;
    DWORD d= 0;
    DWORD ret = 0;
    PACL pACL = NULL; 
     EXPLICIT_ACCESS ea[1]; 
     SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; 
     
     // Create a well-known SID for the Everyone group. 
     a = AllocateAndInitializeSid(&SIDAuthWorld, 1, 
             SECURITY_WORLD_RID, 
             0, 0, 0, 0, 0, 0, 0, 
             &pEveryoneSID); 
    if(!a)
    DbgOut("SID initialization failed");
     // Initialize an EXPLICIT_ACCESS structure for an ACE. 
     // The ACE will allow Everyone read access to the key. 
     ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS)); 
     ea[0].grfAccessPermissions = 0xFFFFFFFF; 
     ea[0].grfAccessMode = GRANT_ACCESS; 
     ea[0].grfInheritance= NO_INHERITANCE; 
     ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; 
     ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; 
     ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID; 
     
     // Create a new ACL that contains the new ACEs. 
     ret = SetEntriesInAcl(1, ea, NULL, &pACL);  if(ret != ERROR_SUCCESS)
    DbgOut("ACL entry failed");

     // Initialize a security descriptor. 
     PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, 
                  SECURITY_DESCRIPTOR_MIN_LENGTH); 
     
     b = InitializeSecurityDescriptor(pSD,SECURITY_DESCRIPTOR_REVISION); 
    if(!b)
    DbgOut("Security Descriptor initialization failed");
     
     // Add the ACL to the security descriptor. 
     c = SetSecurityDescriptorDacl(pSD, 
        TRUE,  // bDaclPresent flag 
        pACL, 
        FALSE); // not a default DACL 
    if(!c)
    DbgOut("SetSecurityDescriptorDacl failed");
     
     //Change the security attributes 
     // d = SetFileSecurity(FileName, DACL_SECURITY_INFORMATION, pSD); 
    SetNamedSecurityInfo(L"C:\\Program Files\\Mydir\\My.log",SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,pSD,NULL,NULL,NULL);
    if(d != ERROR_SUCCESS)
    DbgOut("SetFileSecurity failed");
     if (pEveryoneSID) 
      FreeSid(pEveryoneSID); 
     if (pACL) 
      LocalFree(pACL); 
     if (pSD) 
      LocalFree(pSD); 
    }
      

  2.   

    怎么会没例子?DirectorySecurity dSecurity = dInfo.GetAccessControl();
    http://msdn.microsoft.com/zh-cn/library/system.security.accesscontrol.directorysecurity(v=vs.80).aspx
      

  3.   

    引入命名空间:using System.Security.AccessControl;
    DirectorySecurity sec = new DirectorySecurity();
    FileSystemAccessRule rule = new FileSystemAccessRule("administrator", FileSystemRights.Delete, AccessControlType.Allow);
    sec.AddAccessRule(rule);
    Directory.CreateDirectory(@"f:\ccc", sec);
    在f:\下创建ccc目录,给予administrator一个删除权限。你在msdn中查下相关这几个类和方法的说明。
      

  4.   

    移除权限,先构造一个
    FileSystemAccessRule 构造函数