Pure.Krome
Pure.Krome

Reputation: 86987

How can I enumerate through a folder to get all file names in an ASP.NET MVC website?

How can I see all the images (*.jpg and *.png) in a folder, in my website?

All I can give is the location of the folder, but that is relative.

eg. ~/Images/Uploaded or /Images/Uploaded.

cheers :)

Upvotes: 1

Views: 3831

Answers (3)

Serge Wautier
Serge Wautier

Reputation: 21888

Jon beats me to it but it may be worth mentioning the overload that takes a search pattern:

string[] files = Directory.GetFiles(Server.MapPath(path), "*.png"); 

You'll need to mix the results for the 2 image types.

Upvotes: 6

Yahia
Yahia

Reputation: 70379

Not sure what exactly you want to do BUT you need to first resolve the relative path to a "real one" using HttpContext.Current.Server.MapPath - for an example see http://www.dotnetperls.com/mappath

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502016

It sounds like you want:

var files = Directory.GetFiles(Server.MapPath(path));

In other words:

Upvotes: 7

Related Questions