如何使用C#授予文件夹及其子文件夹中所有文件的读取权限?

5
如何使用c#在Windows 7中授予文件夹及其子文件夹中的所有文件读取权限?
我不拥有某些文件,但我拥有管理员权限。

请发布你目前所拥有的代码。 - Nasreddine
1
好像是为所有用户授权递归授权的组合。 - Raymond Chen
1个回答

2
DirectoryInfo dirInfo = new DirectoryInfo(@"c:\yourDir");
DirectorySecurity dirSecurity = dirInfo.GetAccessControl();

dirSecurity.AddAccessRule(new FileSystemAccessRule
    (Enviroment.Username,
    FileSystemRights.ReadData, AccessControlType.Allow));

dirInfo.SetAccessControl(dirSecurity);

2
我认为这个问题是关于NTFS权限,而不是.NET权限。 - CodesInChaos
@CodeInChaos 谢谢您的纠正,我已经做出了相应的修改。 - user596075
@Shark,我在GetAccessControl上遇到了未经授权的异常。我不是文件的所有者,但我可以从文件的属性窗口中获取所有权。不知道为什么我不能从C#中获取所有权。 - Rohit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接