Search code examples
c#bitmappathenvironment-variablesimage-file

Second path fragment must not be a drive or UNC name


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.


Solution

  • 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