c#中为文件夹设置权限

 1 ///   <summary>
 2          ///  为创建的临时文件分配权限
 3          ///   </summary>
 4          ///   <param name="pathname"></param>
 5          ///   <param name="username"></param>
 6          ///   <param name="power"></param>
 7          ///   <remarks> SKY 2007-8-6 </remarks>

 8          public   void  addpathPower( string  pathname,  string  username,  string  power)
 9          {
10
11             DirectoryInfo dirinfo  =   new  DirectoryInfo(pathname);
12
13              if  ((dirinfo.Attributes  &  FileAttributes.ReadOnly)  !=   0 )
14              {
15                 dirinfo.Attributes  =  FileAttributes.Normal;
16             }

17
18              // 取得访问控制列表
19             DirectorySecurity dirsecurity  =  dirinfo.GetAccessControl();
20
21              switch  (power)
22              {
23                  case   " FullControl " :
24                     dirsecurity.AddAccessRule( new  FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
25                      break ;
26                  case   " ReadOnly " :
27                     dirsecurity.AddAccessRule( new  FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
28                      break ;
29                  case   " Write " :
30                     dirsecurity.AddAccessRule( new  FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
31                      break ;
32                  case   " Modify " :
33                     dirsecurity.AddAccessRule( new  FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
34                      break ;
35             }

36         }

DirectoryInfo是需要实例化的,而且实例化的时候必须指定文件夹路径,Directory则是静态类.

主程序中调用的写法:

1 private   void  CreateDirectory()
2 {
3    
4    addpathPower(sPath,  " ASPNET " " FullControl " );
5    
6 }
http://www.cnblogs.com/leosky2008/archive/2007/08/08/847405.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值