惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
The Cloudflare Blog
IT之家
IT之家
V
V2EX
雷峰网
雷峰网
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
A
About on SuperTechFans
B
Blog
月光博客
月光博客
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI

博客园 - chy710

隐藏ASP.NET站点的head信息(Server/X-Powered-By/X-Aspnet-Version) Jenkins持续集成 入门实践 Docker入门实践 程序员该有的职业素养 ubuntu sudoers配置错误 MVC Json方法里的一个坑 日志分析工具 Log Parser 压力测试记录 Winscp使用sudo user登录 Linux下通过NFS共享文件夹 Jexus 5.4.6 on CentOS 6.6 数据传输过程的安全考虑 RSA密钥的跨平台通用 MongoDB中的字段类型Id Nginx运行Mono Web (ASP.NET) CentOS升级MySQL到5.5 写给自己看的Linux运维基础(四) - python环境 Python模拟HTTP Post上传文件 Navicat for MySQL的服务器连接管理
C#修改文件权限
chy710 · 2013-09-11 · via 博客园 - chy710

用户名的格式为:Local MachineName\AccountName

机器名可通过System.Environment.MachineName获取。

获取一个文件的权限(帐号)列表

FileSecurity fsec = new FileInfo(path).GetAccessControl();
AuthorizationRuleCollection ar= fsec.GetAccessRules(truetruetypeof(System.Security.Principal.NTAccount));
foreach (AuthorizationRule r in ar)
    r.IdentityReference.Value;


给一个文件赋于IIS帐号的写权限

FileSecurity fsec = new FileInfo(path).GetAccessControl();
fsec.SetAccessRule(new FileSystemAccessRule(@"WEB-01\IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));

设置文件夹的权限

DirectoryInfo dinfo = new DirectoryInfo(path);
DirectorySecurity dsecurity = dinfo.GetAccessControl();
dsecurity.AddAccessRule(new FileSystemAccessRule(@"WEB-01\IIS_IUSRS", FileSystemRights.Write, AccessControlType.Allow));