C#创建文件夹并设置权限

 

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*  
InBlock.gif需要添加以下命名空间:  
InBlock.gifusing System.IO;  
InBlock.gifusing System.Security.AccessControl;  
ExpandedBlockEnd.gif
*/
  
None.gif  
None.gif
string  sPath  =  Server.MapPath(文件夹名称字符串);   
None.gifDirectory.CreateDirectory(sPath);   
None.gifaddpathPower(sPath, 
" ASPNET " " FullControl " );   
None.gif  
ExpandedBlockStart.gifContractedBlock.gif
/**/ //   
None.gif   
None.gif
public   void  addpathPower( string  pathname,  string  username,  string  power)   
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {   
InBlock.gif  
InBlock.gif    DirectoryInfo dirinfo 
= new DirectoryInfo(pathname);   
InBlock.gif  
InBlock.gif    
if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)   
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   
InBlock.gif        dirinfo.Attributes 
= FileAttributes.Normal;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif  
InBlock.gif    
//取得访问控制列表   
InBlock.gif
    DirectorySecurity dirsecurity = dirinfo.GetAccessControl();   
InBlock.gif  
InBlock.gif    
switch (power)   
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{   
InBlock.gif        
case "FullControl":   
InBlock.gif            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));   
InBlock.gif            
break;   
InBlock.gif        
case "ReadOnly":   
InBlock.gif           dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));   
InBlock.gif            
break;   
InBlock.gif        
case "Write":   
InBlock.gif            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));   
InBlock.gif            
break;   
InBlock.gif        
case "Modify":   
InBlock.gif            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));   
InBlock.gif            
break;   
ExpandedSubBlockEnd.gif    }
   
InBlock.gif    dirinfo.SetAccessControl(dirsecurity);   
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/LiveStar/archive/2008/07/25/1251296.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值