Second path fragment must not be a drive or UNC name

user6841064 picture user6841064 · Jul 8, 2017 · Viewed 8.2k times · Source

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.

Answer

Samvel Petrosov picture Samvel Petrosov · Jul 8, 2017

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