3

I am just a newbie to VS, C# and XAML. I have this project am building which I want to display all images in my D;\ drive. So I actually got this code from this question and luckily for me I had it working without any difficulty. I can tell the code is working because my app now takes about 2 minutes to start up, so its been delayed due to the searched images

public static IEnumerable<string> GetDirectoryFiles(string rootPath, string patternMatch, SearchOption searchOption)
{
    var foundFiles = Enumerable.Empty<string>();
    if (searchOption == SearchOption.AllDirectories)
    {
        try
        {
            IEnumerable<string> subDirs = Directory.EnumerateDirectories(rootPath);
            foreach (string dir in subDirs)
            {
                foundFiles = foundFiles.Concat(GetDirectoryFiles(dir, patternMatch, searchOption));
            }
        }
        catch (UnauthorizedAccessException) { }
        catch (PathTooLongException) { }
    }
    try
    {
        foundFiles = foundFiles.Concat(Directory.EnumerateFiles(rootPath, patternMatch));
    }
    catch (UnauthorizedAccessException) { }
    return foundFiles;
}

I use this line of code to call the function GetDirectoryFiles

string[] filePaths = {};
string[] extObj = { "*.JPG", ".JPEG", ".PNG", ".GIF", ".BMP*.jpg", ".jpeg", ".png", ".gif", ".bmp" };
foreach(var ext in extObj)
    filePaths.Concat(GetDirectoryFiles(@"D:\", ext, SearchOption.AllDirectories));
System.Diagnostics.Debug.WriteLine(filePaths.Length);

But am having an issue... When the filePaths.Length is outputted I get 0. I don't actually know why but I know I have at least 4000 .jpg images in my D:\ drive, so I shouldn't be getting 0.

My question in a nutshell: I want to load all images in my D:\ drive excluding the paths which returns an UnauthorizedAccessException and PathTooLongException error in reference to my previous question

KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31
  • You're passing the same file extension twice for no reason: the search pattern is not case sensitive. You'll get back the same file twice. You also have a type here: `".BMP*.jpg"`. See if that's actually a typo just here. – Jimi Jan 13 '19 at 07:12

2 Answers2

6

The Concat extension method returns a new enumerable object but you're not doing anything with it. You need to assign it back to filePaths.

First, change the type of filePaths. If you keep it as an array, you'll have to keep reifying it (e.g. calling ToArray) each time and that's costly.

IEnumerable<string> filePaths = Enumerable.Empty<string>();

Then, assign each new enumerable back to filePaths.

foreach (var ext in extObj)
    filePaths = filePaths.Concat(GetDirectoryFiles(@"D:\", ext, SearchOption.AllDirectories));

Finally, filePaths is IEnumerable<string> so you have to use Count() instead of Length.

System.Diagnostics.Debug.WriteLine(filePaths.Count());

...or just reify it...

string[] finalFilePaths = filePaths.ToArray();
System.Diagnostics.Debug.WriteLine(finalFilePaths.Length);
madreflection
  • 4,744
  • 3
  • 19
  • 29
5

try

    foreach(var ext in extObj)
        filePaths= filePaths.Concat(GetDirectoryFiles(@"D:\", ext, SearchOption.AllDirectories));

you forgot to assign the result of concat to the same source

Vilsad P P
  • 1,529
  • 14
  • 23