【问题标题】:Add recursive command to set sub directory permissions in C#添加递归命令以在 C# 中设置子目录权限
【发布时间】:2013-08-23 01:26:05
【问题描述】:

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

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# recursion


    【解决方案1】:

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

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

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

    我忘记了收益功能是什么时候添加的,但它已经有一段时间了。

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2018-04-26
      • 2012-11-15
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      相关资源
      最近更新 更多