C#文件/目录权限

我正在编写一个应用程序来管理用户对文件的访问。 一个很长的故事的简短版本是我必须使用目录和文件priveleges来做到这一点。 我们便宜的CEO没有文件管理系统……

无论如何……除了用户可以查看目录中的哪些文件但实际上没有看到文件内容的情况之外,我还能正常工作。 (文件中可能存在敏感的HR数据。)

我尝试了FileSystemRights.ListDirectory,但似乎(dispite MS文档)也将ReadData设置为true。 我关闭ReadData(读取文件的能力),我突然无法再次访问该目录。 两者似乎有联系。

有权获得许可的想法吗?

我目前的代码是:

SetSecurity(pth, usr, FileSystemRights.ListDirectory, AccessControlType.Allow); ... public void SetSecurity(string dirName, string account, FileSystemRights rights, AccessControlType controlType) { // Get a FileSecurity object that represents the // current security settings. DirectorySecurity dSecurity = Directory.GetAccessControl(dirName); dSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType)); // Set the new access settings. Directory.SetAccessControl(dirName, dSecurity); } 

谢谢。

– 杰瑞

FileSystemRights枚举将ReadData和ListDirectory映射到值1,因此就.NET而言,这两者是100%等效的。

您是否尝试过Traverse而不是ListDirectory?

编辑:根据这篇知识库文章,似乎Windows XP认为它们也是相同的,只有一个适用于文件,一个仅适用于目录。

编辑2:只要将ReadData / ListDirectory访问规则设置为不被子对象inheritance,您就应该能够将其应用于目录而不将其应用于目录中的文件。 FileSystemAccessRule类支持更改inheritance标志。

这些文件可能是从父级inheritance安全属性。

在调用Directory.SetAccessControl()之前,您可以尝试调用DirectorySecurity.SetAccessRuleProtection(true,false)来阻止文件inheritance。

是的。 Traverse(我认为它的名字错误)允许我在文件夹中执行程序,但不能查看文件夹的内容。 老实说,不确定为什么这是有用的。

我即将告诉首席执行官,它无法完成,并再次观看火花。 :P

它是在实例化FileSystemAccessRule时未设置的inheritance和传播值。