1

so what i'm trying to do is to get all the file names of all the images that are located in a specific folder in my project or on the server (which is practically the same) in a string array.

I tried with

Directory.GetFiles(Server.MapPath(@"~/_img/_upload/"));

But the array stays empty. Could anyone help me with that?

I'd appreciate the help, thanks in advance!

Some Developer
  • 229
  • 5
  • 19

2 Answers2

1

The below will get all the images into an array (if you already know the directory structure this should help you):

using System.IO;
using System.Linq;

var imagenames = String.Join(", ", Directory.GetFiles(@"C:\yourdirectory", "*.img").Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());
03Usr
  • 3,165
  • 6
  • 32
  • 59
0

In base at your comment, the result you got from this Server.MapPath(@"~/_img/_upload/") is:

C:\\Projects\\VS12\_img\_upload\\

The main problem is the @ because in C# means that the string should be used "as is" and not be processed for escape sequences.

Without it, your string value will be: \\_img\\_upload\\

For future reading, I would suggest this answer that I found very useful

Community
  • 1
  • 1
Gianni B.
  • 2,551
  • 15
  • 30