1、放一个按钮,在单击事件里选择一个文件目录,如下:
private void Button_Click(object sender, RoutedEventArgs e) { CommonOpenFileDialog dialog = new CommonOpenFileDialog(); dialog.IsFolderPicker = true;//设置为选择文件夹 if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { this.txtPath.Text = dialog.FileName; } AddSecurityControll2Folder(this.txtPath.Text); MessageBox.Show("添加IIS网站目录权限成功!"); }
2、执行目录权限修改的方法,方法如下:
static void AddSecurityControll2Folder(string dirPath) { DirectoryInfo dir = new DirectoryInfo(dirPath); //获得该文件夹的所有访问权限 System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All); //设定文件ACL继承 InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; FileSystemAccessRule IIS_IUSRS_FileSystemAccessRule = new FileSystemAccessRule("IIS_IUSRS", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow); FileSystemAccessRule IUSR_FileSystemAccessRule = new FileSystemAccessRule("IUSR", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow); bool isModified = false; dirSecurity.ModifyAccessRule(AccessControlModification.Add, IIS_IUSRS_FileSystemAccessRule, out isModified); dirSecurity.ModifyAccessRule(AccessControlModification.Add, IUSR_FileSystemAccessRule, out isModified); dir.SetAccessControl(dirSecurity); }