2009-02-03 76 views
2

作爲Vista SP1上的提升管理員運行,我的C#應用​​程序嘗試使用以下代碼設置以下規則。沒有產生錯誤,但是目錄的ACL沒有任何改變。我錯過了什麼?爲什麼我不能在C#中設置這個ACL規則?

public static void Main(string args[]) 
{ 
    string dirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Company"), "Product"); 
    Directory.Create(dirPath); 
    _SetAcl(dirPath, "Users", FileSystemRights.FullControl); 
} 

private static void _SetAcl(string path, string identity, FileSystemRights rights) 
{ 
    var info = new DirectoryInfo(path); 
    var acl = info.GetAccessControl(); 

    var rule1 = new FileSystemAccessRule(identity, rights, AccessControlType.Allow); 
    bool modified; 
    acl.ModifyAccessRule(AccessControlModification.Reset, rule1, out modified); 

    var inheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; 
    var rule2 = new FileSystemAccessRule(identity, rights, inheritanceFlags, 
             PropagationFlags.InheritOnly, AccessControlType.Allow); 
    acl.ModifyAccessRule(AccessControlModification.Add, rule2, out modified); 
} 

更新:只需添加以下代碼作爲_SetAcl方法的最後一行,我的代碼是好去。

info.SetAccessControl(acl); 

回答

7

要完成該過程,必須使用修改後的ACL調用DirectoryInfo.SetAccessControl()。

GetAccessControl()確實返回ACL的副本。你可以自由修改它,但是直到你調用SetAccessControl()

+0

D'oh!它纔會生效。那樣做了。謝謝。 – flipdoubt 2009-02-03 14:33:37

相關問題