1 private void SetRight(string folderpath)
2 {
3 bool result = false;
4 DirectoryInfo folder = new DirectoryInfo(folderpath);
5 folder.Attributes |= FileAttributes.Normal;
6 folder.Attributes &= ~FileAttributes.Hidden; // remove the folder Hidden attribute
7 folder.Attributes &= ~FileAttributes.ReadOnly;// remove the folder ReadOnly attribute
8 DirectorySecurity foldersecurity = new DirectorySecurity();
9 FileSystemAccessRule filerule = new FileSystemAccessRule("INTERACTIVE", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
10 FileSystemAccessRule filerule1 = new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
11 FileSystemAccessRule filerule2 = new FileSystemAccessRule("NETWORK", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
12 foldersecurity.ModifyAccessRule(AccessControlModification.Add, filerule, out result);
13 foldersecurity.ModifyAccessRule(AccessControlModification.Add, filerule1, out result);
14 foldersecurity.ModifyAccessRule(AccessControlModification.Add, filerule2, out result);
15 folder.SetAccessControl(foldersecurity);
16 }
注:folderpath为文件夹路径,将需要获得权限的文件夹路径传入方法即可。