-4

i have this piece of code

string [] ImgLocation =
    Directory.GetFiles(@"Assets\Cards\Pack_Classic\", " *.png",
                       SearchOption.TopDirectoryOnly);

it's supposed to give me the location of all the image files inside the folder.However it doesn't work at all, it just gives me 0 strings. Why is that ? The images location is : _Poker\Poker\bin\Debug\Assets\Cards\Pack_Classic

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
kopelence
  • 173
  • 1
  • 2
  • 9

1 Answers1

1

GetFiles needs an absolute file path in order to work in a reliable way. Get it from Reflection (through the Assembly class)

string exeDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string fullPath = Path.Combine(exeDir, @"Assets\Cards\Pack_Classic");
string[] ImgLocation = Directory.GetFiles(fullPath, "*.png", SearchOption.TopDirectoryOnly);

exeDir is your bin\Debug folder.


Note: GetFiles works also with relative paths starting at the current working directory. The problem is that you don't always know where that one is! It can be different from the directory where the executable resides.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188