דניאל רשת
דניאל רשת

Reputation: 109

How can i make the method to return a List of files only and not also directories?

In constructor. filePathList is List

filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null).ToList();

And the SearchAccessibleFilesNoDistinct method

IEnumerable<string> SearchAccessibleFilesNoDistinct(string root, List<string> files)
        {
            if (files == null)
                files = new List<string>();
            if (Directory.Exists(root))
            {
                foreach (var file in Directory.EnumerateFiles(root))
                {
                    string ext = Path.GetExtension(file);
                    if (!files.Contains(file) && ext == textBox2.Text)
                    {
                        files.Add(file);
                    }
                }
                foreach (var subDir in Directory.EnumerateDirectories(root))
                {
                    try
                    {
                        SearchAccessibleFilesNoDistinct(subDir, files);
                        files.Add(subDir);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        // ...
                    }
                }
            }
            return files;
        }

Then i make loop over the List filePathList

foreach (string file in filePathList)
            {
                try
                {
                    _busy.WaitOne();
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        return;
                    }

                    bool reportedFile = false;

                    for (int i = 0; i < textToSearch.Length; i++)
                    {
                        if (File.ReadAllText(file).IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            resultsoftextfound.Add(file + "  " + textToSearch[i]);
                            if (!reportedFile)
                            {
                                numberoffiles++;
                                MyProgress myp = new MyProgress();
                                myp.Report1 = file;
                                myp.Report2 = numberoffiles.ToString();
                                myp.Report3 = textToSearch[i];
                                backgroundWorker1.ReportProgress(0, myp);
                                reportedFile = true;
                            }
                        }
                    }
                    numberofdirs++;
                    label1.Invoke((MethodInvoker)delegate
                    {
                        label1.Text = numberofdirs.ToString();
                        label1.Visible = true;
                    });
                }
                catch (Exception)
                {
                    restrictedFiles.Add(file);
                    numberofrestrictedFiles ++;
                    label11.Invoke((MethodInvoker)delegate
                    {
                        label11.Text = numberofrestrictedFiles.ToString();
                        label11.Visible = true;
                    });
                    continue;
                }

The problem is that in the catch part the restrictedFiles is just directories not files. Since filePathList contain files and directories and when it's trying to search in a directory it's getting to the catch. It's not a restricted file it's just directory and not file at all.

That's why i think the method SearchAccessibleFilesNoDistinct should return only files without directories as items.

For example in filePathList i see in index 0:

c:\temp

And in index 1

c:\temp\help.txt

The first item in index 0 will go to the catch as restricted file and the second item will not.

Upvotes: 0

Views: 55

Answers (2)

Jim Mischel
Jim Mischel

Reputation: 134045

You have this loop to search search the subdirectories:

            foreach (var subDir in Directory.EnumerateDirectories(root))
            {
                try
                {
                    SearchAccessibleFilesNoDistinct(subDir, files);
                    files.Add(subDir);  <--- remove this line
                }
                catch (UnauthorizedAccessException ex)
                {
                    // ...
                }
            }

Remove the line that adds the subdirectory to the list of files. I've marked it in the code above.

Upvotes: 2

Arheus
Arheus

Reputation: 192

Is it you looking for:

        Directory.GetFiles(rootDir,"*.*", SearchOption.AllDirectories);

? Change . to mask form textbox only.

Upvotes: 2

Related Questions