5

How to read all image files in folder by environment path folder in main folder:

   string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "mainFolder"); 

for example if inner folder for one file path is:

  Bitmap bmp = new Bitmap(path + "/folder/pic1.bmp");

but I want read all files of mainFolder/folder:

pic1.bmp
pic2.bmp
pic3.bmp
pic5.bmp

Not sure how to do it properly:

foreach (string imageFileName in Directory.GetFiles(path, "/folder/*.bmp"))
{
    using (Bitmap bmp = new Bitmap(imageFileName))
    {
      // process 
    }
}

this way I have: Second path fragment must not be a drive or UNC name

and this way:

foreach (string imgFileName in Directory.GetFiles(path + "/folder/*.bmp"))

I got: Illegal characters in path.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34

3 Answers3

3

This code fails because you must provide file name's pattern as a second parameter:

foreach (string imgFileName in Directory.GetFiles(path, "/folder/*.jpg"))

The second one fails because '*' is a special symbol and UNC paths don't accept that.

foreach (string imgFileName in Directory.GetFiles(path + "/folder/*.jpg"))

So you can try to do the following:

foreach (string imgFileName in Directory.GetFiles(path + "/folder/","*.jpg")) 

MSDN Directory.GetFiles

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
  • ,Hello, yes that's right, I need comma there for path,but there is two things which must be taking into the account, first of all I need to count all files to avoid endless loop, which is not connected to my question directly. but also it seem like it does not switches from file to file, and gives me information from same one, which is first in the folder –  Jul 08 '17 at 16:39
  • @rayho I have checked this code but with pattern `"*.pdf"` as I have not folder with `.jpg` images. And it returns exactly all files in that folder. Also I don't understand why do you need to count files? But even if you need you can just take `Directory.GetFiles(path + "/folder/","*.jpg").Length` – Samvel Petrosov Jul 08 '17 at 16:55
  • @rayho Also I hope that you won't do any renaming and creations of files while going on this `foreach` loop, otherwise I suggest you to take the list of files into array and do that in `for` loop to avoid endless loops – Samvel Petrosov Jul 08 '17 at 17:00
  • @rayho can you please just write to console the `imgFileName` in that loop and add to the question – Samvel Petrosov Jul 08 '17 at 17:08
  • @ Samvel Petrosov Yes of course edited, I'm just count black and white pixels for each –  Jul 08 '17 at 17:24
  • @rayho I mean add to the question the values you are getting as imgFileName while loop's iteration – Samvel Petrosov Jul 08 '17 at 17:26
  • and I change it to bmp to make it more easy, and output is a endless loop with first file information. added to post –  Jul 08 '17 at 17:30
  • @rayho can you please run this code `string allFiles = string.Empty; foreach (string imageFileName in Directory.GetFiles(basePath + "/base/", "*.bmp")) { allFiles += imageFileName + "\r\n"; } MessageBox.Show(allFiles);` and tell me what you get as a result in messagebox – Samvel Petrosov Jul 08 '17 at 17:33
  • @rayho I have tried your code on my end and take a look at the result http://take.ms/1Dkji – Samvel Petrosov Jul 08 '17 at 17:45
  • @rayho as you can see there are 3 different result for 3 images but also as we can see the count of colors is counter not correct – Samvel Petrosov Jul 08 '17 at 17:46
  • @ Samvel Petrosov Absolutely same, and you have my desired result, but my result is different, and I can't get why... –  Jul 08 '17 at 17:48
  • @rayho in this case the only way I think is that you try to debug and step by step checking all the variables get out what is the reason, because on my end it's working for all images, but off course with incorrect counted values :) – Samvel Petrosov Jul 08 '17 at 17:50
  • @rayho no but I will do it now – Samvel Petrosov Jul 08 '17 at 17:52
  • @rayho I got the same result as for common folder – Samvel Petrosov Jul 08 '17 at 17:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148694/discussion-between-samvel-petrosov-and-rayho). – Samvel Petrosov Jul 08 '17 at 17:55
0

Directory.GetFiles() does not accept asterisks in its path name. What you can do, is provide the file extension as a searchpattern:

foreach(string filename in Directory.GetFiles(Path.Combine(path, "folder"), "*.jpg"))
    //Do your stuff here

Further reading

Max Play
  • 3,717
  • 1
  • 22
  • 39
  • ,Hello, this way does not takes into account the inner `mainFolder/folder` –  Jul 08 '17 at 16:20
  • yes as answer to get the path it is also correct, but as I answered above, my method is not really correct for this case to get desired result, because it is does not switches from file to file, and shows information from one same file, which is a first in folder –  Jul 08 '17 at 16:48
0

GetFiles will do what you need. You could write a new method that returns a String[] of the files found. This would allow you to use it elsewhere if required.

public String[] GetFilesFromFolder(String folder, String[] filters, bool recursiveSearch)
 {
    List<String> foundFiles= new List<String>();
    var searchingOption = recursiveSearch ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
    foreach (var filter in filters)
    {
       foundFiles.AddRange(Directory.GetFiles(folder, String.Format("*.{0}", filter), searchOption));
    }
    return foundFiles.ToArray();
 }

Then to use it, just do something like below:

String folder= "PATH TO FOLDER";
var filters = new String[] { "bmp", "jpg", "jpeg", "png" };
var files = GetFilesFromFolder(folder, filters, false);

This also gives you control over searching for any file type you like, so not just BMP, but JPG, PNG or anything else (Doc, Xls etc)

Paul Stringer
  • 90
  • 2
  • 10