栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用C#.NET将“所有人”特权添加到文件夹

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用C#.NET将“所有人”特权添加到文件夹

我想告诉你的第一件事是我如何找到这个解决方案的。这可能比答案更重要,因为文件权限很难正确获得。

我要做的第一件事是使用Windows对话框和复选框设置所需的权限。我为“每个人”添加了一条规则,并勾选了“完全控制”以外的所有框。

然后,我编写了此C#代码,以确切地告诉我复制Windows设置所需的参数:

string path = @"C:UsersyouDesktopperms"; // path to directory whose settings you have already correctly configuredDirectorySecurity sec = Directory.GetAccessControl(path);foreach (FileSystemAccessRule acr in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) {    Console.WriteLine("{0} | {1} | {2} | {3} | {4}", acr.IdentityReference.Value, acr.FileSystemRights, acr.InheritanceFlags, acr.PropagationFlags, acr.AccessControlType);}

这给了我这行输出:

Everyone | Modify, Synchronize | ContainerInherit, ObjectInherit | None | Allow

因此,解决方案很简单(但如果您不知道要寻找什么,就很难正确解决!):

DirectorySecurity sec = Directory.GetAccessControl(path);// Using this instead of the "Everyone" string means we work on non-English systems.SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));Directory.SetAccessControl(path, sec);

这将使Windows安全对话框中的复选框与您已为测试目录设置的复选框匹配。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465380.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号