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
}
一般来说 Username 选用 ASPNET
private void CreateDirectory()2
{3

4
addpathPower(sPath, "ASPNET", "FullControl");5

6
}