1

This is the code in my method :

t = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string[] textfiles = ApplyAllFiles(t, "*.txt", ProcessFile).ToArray();

Then i did also:

s = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string[] textfiles = ApplyAllFiles(s, "*.jpg", ProcessFile).ToArray();

And the ApplyAllFiles method:

static void ProcessFile(string path) {/* ... */}

static IEnumerable<string> ApplyAllFiles(string folder, string searchPattern, Action<string> fileAction)
{
     IEnumerable<string> files = Directory.GetFiles(folder, searchPattern);
     foreach (string file in files)
     {
         fileAction(file);
     }

     foreach (string subDir in Directory.GetDirectories(folder))
     {
         try
         {
             files = files.Concat(ApplyAllFiles(subDir, searchPattern, fileAction));
         }
         catch
         {
              // swallow, log, whatever
         }
      }

      return files;
}
  1. Its not getting files from the main directory for example the first main directory is: C:\Users\bout0_000\Documents And i have in this directory some text files. It will get all the text files from sub
    directories in Documents but it will never get the text file that are in C:\Users\bout0_000\Documents

  2. How can i get more then one extention for example i did "*.jpg" but i want also to get bmp files png files and gif files.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
DanielVest
  • 823
  • 4
  • 20
  • 39
  • For some info on multiple extensions and `Directory.GetFiles()` look at this post: http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters – David Tansey Aug 11 '13 at 04:23
  • I ran your code unchanged on my system and it successfully returns TXT files from the 'main' folder, meaning my `Documents` folder. – David Tansey Aug 11 '13 at 04:30
  • David the textfiles is working but this s = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures); give me : C:\\Users\\Public\\Pictures but i want it to give me like the textfiles the variable t: C:\\Users\\bout0_000\\Documents so Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures); i want it to give me: C:\\Users\\bout0_000\\Pictures how can i do it ? – DanielVest Aug 11 '13 at 04:41
  • `Environment.SpecialFolder.MyPictures` would give you the correct filepath for a given user as opposed to the Public Shared folder. – randcd Aug 11 '13 at 04:49
  • Not sure that I understand what you just asked in the comment, but I think I might. Instead of `Environment.SpecialFolder.CommonPictures` please try `Environment.SpecialFolder.MyPictures` Hope that helps. – David Tansey Aug 11 '13 at 04:50

1 Answers1

1

Why don't you just use SearchOption.AllDirectories? Then you won't need the Concat or the recursion. You could just say

static void ApplyAllFiles(string folder, string searchPattern, Action<string> fileAction)
{
    foreach (string file in Directory.GetFiles(folder, searchPattern, SearchOption.AllDirectories))
        fileAction(file);
}

Here is the overload in question. Or you can use EnumerateFiles.

Unfortunately, the searchPattern argument doesn't support multiple extension patterns, so you'll have to do that yourself. Something like this:

static void ApplyAllFiles(string folder, Action<string> fileAction)
{
    foreach (string file in
             Directory
             .EnumerateFiles(folder, "*", SearchOption.AllDirectories)
             .Where(f => new[] {".jpg", ".txt"}.Contains(Path.GetExtension(f))))
        fileAction(file);
}
harpo
  • 41,820
  • 13
  • 96
  • 131
  • harpo the reason i dont want to use SearchOption.AllDirectories is that for example on my wife laptop im getting exception access denied on some sub directories from Documents and i saw that the solution is not to use SearchOption.AllDirectories but recursion . So i dont want to use SearchOption.AllDirectories at all. – DanielVest Aug 11 '13 at 04:31
  • 2
    If you're getting access denied, then it's not going to matter which method you use. If you need to selectively omit certain directories, you can use a method similar to the second sample, where you individually test file extensions. (Alternatively, you could just have your wife grant your account access :) – harpo Aug 11 '13 at 04:33