2012-05-14 33 views
12

Tôi phải gán quyền trên thư mục và thư mục con cũng như tệp của chương trình bằng C# .NET. Tôi làm như sau:Chỉ định quyền truy cập danh sách điều khiển truy cập (ACL) theo cách lập trình cho 'thư mục, thư mục con và tệp'

var rootDic = @"C:\ROOT"; 
var identity = "NETWORK SERVICE"; //The name of a user account. 
try 
{ 
    var accessRule = new FileSystemAccessRule(identity, 
         fileSystemRights: FileSystemRights.Modify, 
         inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
         propagationFlags: PropagationFlags.InheritOnly, 
         type: AccessControlType.Allow); 

    var directoryInfo = new DirectoryInfo(rootDic); 

    // Get a DirectorySecurity object that represents the current security settings. 
    DirectorySecurity dSecurity = directoryInfo.GetAccessControl(); 

    // Add the FileSystemAccessRule to the security settings. 
    dSecurity.AddAccessRule(accessRule); 

    // Set the new access settings. 
    directoryInfo.SetAccessControl(dSecurity); 
} 
catch (Exception ex) 
{ 
    //... 
} 

Nó chỉ định quyền trên thư mục 'C: \ ROOT' của tôi. Nhưng nó chỉ gán quyền cho các Thư mục con và Tập tin nhưng không được phép cho thư mục 'ROOT'. enter image description here

H: Làm cách nào để xác định trường hợp FileSystemAccessRule để gán quyền cho thư mục ROOT, Thư mục con và tệp?

Trả lời

11

Bạn chỉ cần xóa cờ PropagationFlags.InheritOnly. Bằng cách xác định rằng bạn đang nói rằng ACE không nên áp dụng cho thư mục đích. Sử dụng PropagationFlags.None để thay thế. Bạn có thể tìm thấy điều này MSDN article hữu ích.

+0

@Kibria Tôi rất tò mò. Trợ giúp này có hữu ích không? –

+1

có, cảm ơn! – Kibria

Các vấn đề liên quan