简体   繁体   English

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

[英]Add recursive command to set sub directory permissions in C#

I would like to add a recursive command to this script that allows it to loop through a current direcotries sub directory/files and set the permissions on the subfolders/files to whatever I would like. 我想向此脚本添加一个递归命令,以使其可以循环浏览当前目录子目录/文件,并将子文件夹/文件的权限设置为我想要的任何值。 Here is what I have so far which allows for the permissions to be changed on the first set of subdirectories. 到目前为止,这是我可以在第一组子目录上更改权限的内容。 Obviously, I can add the samecode in to keep diving down through the folder structure, but not every root folder will have the same amount of sub folders within it. 显然,我可以添加相同的代码来继续深入研究文件夹结构,但是并不是每个根文件夹中都具有相同数量的子文件夹。 I want to add the recursive command to loop through all subdirectories and when there are no more, move on to the next root folder. 我想添加递归命令以遍历所有子目录,当没有更多子目录时,请移至下一个根文件夹。

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

First, if your target framework is 4.0, recommend that you use the Directory.EnumerateFiles method (you can also find 3rd code that does the same thing.) 首先,如果您的目标框架是4.0,则建议您使用Directory.EnumerateFiles方法(您还可以找到执行相同操作的第3个代码。)

Assuming this is a no-go, you can simplify your recursive processing by using the yield keyword, eg make a traverse method based on yield -- I'm showing this with a filter function to since it would often be useful in directory traversal ad should give you ideas. 假设这是不行的,您可以使用yield关键字来简化递归处理,例如,基于yield产生遍历方法-我将其与filter函数一起展示,因为它通常在目录遍历广告中很有用应该给你的想法。

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

Then you use traversal() this way 然后您以这种方式使用traversal()

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

You will have a more easily reusable directory traversal at your fingertips. 您将触手可及,可以更轻松地重用目录遍历。 I know that I am not yielding directories in the snippet above, but if I wanted to process both files and directory, I would base this on the DirectoryInfo.GetFileSystemInfos method instead. 我知道我没有在上面的代码段中产生目录,但是如果我想同时处理文件和目录,则可以基于DirectoryInfo.GetFileSystemInfos方法。

I forget when the yield feature was added, but it has been available for quite a while. 我忘记了何时添加yield功能,但是已经有一段时间了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM