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

/*  
需要添加以下命名空间:  
using System.IO;  
using System.Security.AccessControl;  
*/
  
  
string sPath = Server.MapPath(文件夹名称字符串);   
Directory.CreateDirectory(sPath);   
addpathPower(sPath, 
"ASPNET""FullControl");   
  
//////////////////////////////////////////////////   
  
public void addpathPower(string pathname, string username, string power)   
{   
  
    DirectoryInfo dirinfo 
= new DirectoryInfo(pathname);   
  
    
if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)   
    
{   
        dirinfo.Attributes 
= FileAttributes.Normal;   
    }
   
  
    
//取得访问控制列表   
    DirectorySecurity dirsecurity = dirinfo.GetAccessControl();   
  
    
switch (power)   
    
{   
        
case "FullControl":   
            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));   
            
break;   
        
case "ReadOnly":   
           dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));   
            
break;   
        
case "Write":   
            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));   
            
break;   
        
case "Modify":   
            dirsecurity.AddAccessRule(
new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));   
            
break;   
    }
   
    dirinfo.SetAccessControl(dirsecurity);   
}
posted @   与时俱进  阅读(7623)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
友情链接:同里老宅院民居客栈
点击右上角即可分享
微信分享提示