添加递归命令以在C#中设置子目录权限

本文关键字:设置 子目录 权限 递归 命令 添加 | 更新日期: 2023-09-27 18:28:47

我想向该脚本添加一个递归命令,使其能够循环通过当前目录子目录/文件,并将子文件夹/文件的权限设置为我想要的任何权限。以下是到目前为止我所拥有的内容,它允许更改第一组子目录的权限。显然,我可以在中添加相同的代码来继续深入文件夹结构,但并不是每个根文件夹中都有相同数量的子文件夹。我想添加递归命令来遍历所有子目录,当没有子目录时,转到下一个根文件夹。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;
using System.Management;
using System.Management.Instrumentation;
namespace ApplyPermissions
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void label1_Click(object sender, EventArgs e)
        {
        }
        private void selectDirectoryBtn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
            myFolderBrowserDialog.ShowDialog();
            selectedDirBox.Text = myFolderBrowserDialog.SelectedPath.ToString();
            try
            {
                DirectoryInfo myDirectoryInfo = new DirectoryInfo(selectedDirBox.Text);
                foreach (DirectoryInfo currentDir in myDirectoryInfo.GetDirectories())
                {
                    toolStripStatusLabel1.Text = currentDir.Name;
                    DirectorySecurity DirSecurity = currentDir.GetAccessControl();
                    DirSecurity.AddAccessRule(new FileSystemAccessRule(“Whatever permissions group I choose”, FileSystemRights.CreateFiles, AccessControlType.Allow));                
                    currentDir.SetAccessControl(DirSecurity);
                    // Step thru each file within current Directory and assign access
                    foreach (FileInfo currentFile in currentDir.GetFiles())
                    {
                        FileSecurity fileSecurity = currentFile.GetAccessControl();
                        fileSecurity.AddAccessRule(new FileSystemAccessRule("Whatever permissions group I choose", FileSystemRights.FullControl, AccessControlType.Allow));
                        currentFile.SetAccessControl(fileSecurity);
                    }
                    foreach (DirectoryInfo subDir in currentDir.GetDirectories ())
                    {

                        toolStripStatusLabel1.Text = currentDir.Name + "/" + subDir.Name;
                        DirectorySecurity allsubDirSecurity = subDir.GetAccessControl();
                        allsubDirSecurity.AddAccessRule(new FileSystemAccessRule("Whatever permissions group I choose ", FileSystemRights.FullControl, AccessControlType.Allow));
                        subDir.SetAccessControl(allsubDirSecurity);
                        // Step thru each file within current SubDirectory and assign access
                        foreach (FileInfo currentFile in subDir.GetFiles())
                        {
                            FileSecurity fileSecurity = currentFile.GetAccessControl();
                            fileSecurity.AddAccessRule(new FileSystemAccessRule("Whatever permissions group I choose", FileSystemRights.FullControl, AccessControlType.Allow));
                            currentFile.SetAccessControl(fileSecurity);
                        }
                    }
                }
                labelFinished.Text = "Completed Successfully";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "/////////////////" + ex.StackTrace);
            }
        }
    }
}

添加递归命令以在C#中设置子目录权限

首先,如果您的目标框架是4.0,建议您使用Directory.EnumerateFiles方法(您也可以找到做同样事情的第三个代码。)

假设这是不可行的,你可以通过使用yield关键字来简化递归处理,例如,制作一个基于yield的遍历方法——我用过滤器函数来展示这一点,因为它在目录遍历中通常很有用。广告应该给你一些想法。

static IEnumerable<string> traverse(string path, Func<string, bool> filter)
{
    foreach (string f in Directory.GetFiles(path).Where(filter))
    {
        yield return f;
    }
    foreach (string d in Directory.GetDirectories(path))
    {
        foreach (string f in traverse(d, filter))
        {
            yield return f;
        }
    }
}

然后你用这种方式遍历()

var files = traverse(PATH, WHERE);
foreach (string f in files) { DoWhatever; }

您将拥有一个更容易重复使用的目录遍历触手可及。我知道我不会在上面的代码段中生成目录,但如果我想同时处理文件和目录,我会基于DirectoryInfo.GetFileSystemInfos方法。

我忘了什么时候添加了yield功能,但它已经使用了很长一段时间。