C#創建文件夾並設置權限
原文地址:https://www.cnblogs.com/top5/archive/2010/04/12/1710141.html
/*
需要添加以下命名空間:
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));
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);
}
C#創建文件夾並設置權限