2017-10-11 104 views
0

所以,基本上我有一個問題,這些文件被從幾層上移到文件夾中,並且由於某些原因,子項的權限未被繼承。從我可以告訴這是windows的預期功能,但我需要它的工作不同,所以我決定這樣做:將父文件夾權限複製到子文件

foreach (string directory in System.IO.Directory.GetDirectories(@"path", "*", SearchOption.TopDirectoryOnly)) 
{ 
foreach (string file in System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly)) 
{ 
    DirectorySecurity DS = System.IO.Directory.GetAccessControl(directory); 
    FileSecurity FS = new FileSecurity(); 
    System.IO.FileInfo FI = new FileInfo(file); 
    foreach (FileSystemAccessRule rule in DS.GetAccessRules(true, true, typeof(NTAccount))) 
    { 
     FS.AddAccessRule(rule); 
    } 
    FI.SetAccessControl(FS); 
} 
} 

然而,這是產生錯誤而做「fs.addaccessrule」說:

system.argumentexception no flags can be set 

我不知道我應該如何將權限從父文件夾移動到子文件。

回答

0

這是我想出的解決方案,只是基於我想要使用的規則創建新規則並刪除繼承的標誌。

foreach (string directory in System.IO.Directory.GetDirectories(@"path", "*", SearchOption.AllDirectories)) 
{ 
foreach (string file in System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly)) 
{ 
    DirectorySecurity DS = System.IO.Directory.GetAccessControl(directory, AccessControlSections.Access); 
    FileSecurity FS = new FileSecurity(); 
    System.IO.FileInfo FI = new FileInfo(file); 
    foreach (FileSystemAccessRule rule in DS.GetAccessRules(true, false, typeof(NTAccount))) 
    { 
     FileSystemAccessRule nRule = new FileSystemAccessRule(rule.IdentityReference, rule.FileSystemRights, InheritanceFlags.None, rule.PropagationFlags, rule.AccessControlType); 
     FS.AddAccessRule(nRule); 
    } 
    FI.SetAccessControl(FS); 
} 
} 
相關問題