如何在创建新文件夹时复制安全信息?

dhh*_*dhh 5 .net c# filesystems security

在我的应用程序中,我正在创建用于从硬盘中存档旧内容的文件夹.

创建新文件夹时,我必须将所有NTFS权限(组/用户)从源文件夹复制到新创建的目标文件夹.

这是我到目前为止所写的:

FileSecurity fileSecurity =
    File.GetAccessControl(filenameSource, AccessControlSections.All);
FileAttributes fileAttributes = File.GetAttributes(filenameSource);
File.SetAccessControl(filenameDest, fileSecurity);
File.SetAttributes(filenameDest, fileAttributes);
Run Code Online (Sandbox Code Playgroud)

这真的是我应该做的还是我错过了一些重要的事情?

KMå*_*Mån 2

如果那是一个文件夹,那么您可能需要检查Directory.SetAccessControl()方法。DirectorySecurity.SetAccessRuleProtection(isProtected/*true*/,preserveInheritance /*false*/)如果您想阻止文件继承 ACL 规则,您可能需要在调用 Directory.SetAccessControl(); 之前调用