0

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

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);
            }
        }
    }
}
4

1 回答 1

0

首先,如果您的目标框架是 4.0,建议您使用 Directory.EnumerateFiles 方法(您也可以找到执行相同操作的第 3 个代码。)

假设这是不行的,您可以通过使用 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;
        }
    }
}

然后你以这种方式使用 traversal()

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

您将有一个更容易重用的目录遍历在您的指尖。我知道我没有在上面的代码片段中产生目录,但是如果我想同时处理文件和目录,我会基于 DirectoryInfo.GetFileSystemInfos 方法来代替。

我忘记了何时添加了 yield 功能,但它已经有一段时间了。

于 2013-08-23T01:58:09.813 回答