1 /// <summary> 2 /// 添加文件夹用户和权限 3 /// </summary> 4 /// <param name="pathname">文件夹路径</param> 5 /// <param name="username">用户名称</param> 6 /// <param name="privilege">用户权限0:完全控制1:只读2:写入</param> 7 public void AddPathPri(string pathname, string username, int privilege) 8 { 9 try 10 { 11 DirectoryInfo dirinfo = new DirectoryInfo(pathname); 12 if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0) 13 { 14 dirinfo.Attributes = FileAttributes.Normal; 15 } 16 //取得访问控制列表 17 DirectorySecurity dirsecurity = dirinfo.GetAccessControl(); 18 switch (privilege) 19 { 20 case 0: 21 dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); 22 break; 23 case 1: 24 25 dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow)); 26 break; 27 case 2: 28 dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow)); 29 break; 30 default: 31 dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny)); 32 break; 33 } 34 dirinfo.SetAccessControl(dirsecurity); 35 } 36 catch 37 { 38 //nothing; 39 } 40 }
这个在asp.net上(win7)使用时候提示没有权限
@Longkin: 不会啊,我现在就是win7 IIS7可以用 ,你用administrator应该没问题吧
@晨之风: 你好,能加Q说么,437562503