I
I
iluxa18102016-07-10 01:31:05
.NET
iluxa1810, 2016-07-10 01:31:05

How to quickly traverse directories and get a file tree?

Does it make sense to use parallelism? Some people say that parallelism is crazy to use for such purposes.
In C#, of course, there is a ready-made solution in the form of Directory.EnumerateFiles, but if there are any problems (For example, there will be no rights), then I will not get any elements of the collection at all, and therefore, I think that I need to bypass the pens.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2016-07-10
@yarosroman

maybe it makes sense to get the contents of the directory on request, when accessing the collection, but why is a tree needed?

U
Uncle Seryozha, 2016-09-02
@Protos

Use recursion.
True, I can't write fast code. If you do not have enough rights, then look for windows vulnerabilities.
From the code below you will be interested in the variableList<string> filesanddirecoties;

List<string> filesanddirecoties;
        DirectoryInfo[] files;
        void WalkDirectoryTree(string RootDirectory)
        {
            richTextBox1.Clear();
            DirectoryInfo root = new DirectoryInfo(RootDirectory);
            if (root.Exists)
            {
                try
                {
                    filesanddirecoties = new List<string>();
                    DirectoryInfo[] subDirs = root.GetDirectories();
                    AuthorizationRuleCollection ACLs = root.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                    string rules = "";
                    string[] Tree = root.FullName.Split('\\');

                    for (int i = 0; i < Tree.Length; i++)
                    {
                        filesanddirecoties.Add(Tree[i]);
                        string[] files = Directory.GetFiles(RootDirectory);
                        for (int j = 0; j < files.Length; j++)
                        {
                            if(files.Length!=0)
                                filesanddirecoties.Add(files[j]);
                        }
                    }
                    //получаем права на папку
                    /*foreach (FileSystemAccessRule ACL in ACLs)
                    {
                        if (!IsSystemRules(ACL.IdentityReference.ToString()))
                        {
                            for (int i = 0; i < Tree.Length - 2; i++)
                                rules += "\t";
                            rules += ACL.IdentityReference;

                            if (ACL.AccessControlType.ToString().Equals("Allow"))
                            {
                                rules += ", разрешено: " + ACL.FileSystemRights + Environment.NewLine;
                            }
                            else if (ACL.AccessControlType.ToString().Equals("Deny"))
                            {
                                rules += ", запрещено: " + ACL.FileSystemRights + Environment.NewLine;
                            }
                        }
                    }*/
                    for (int i = 0; i < Tree.Length - 2; i++)
                        richTextBox1.Text += "\t";
                    richTextBox1.Text += RootDirectory + Environment.NewLine + rules + Environment.NewLine;

                    if (Tree.Length < (Convert.ToInt32(textBox2.Text) + 1))
                        foreach (DirectoryInfo dirInfo in subDirs)
                            WalkDirectoryTree(dirInfo.FullName);
                }
                catch (UnauthorizedAccessException ee)
                {
                    richTextBox1.Text += RootDirectory + Environment.NewLine;
                }
            }
            else { richTextBox1.Text = "Папка не существует. Отмена операции."; }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question