I am Developing a Web site and i need to read all the images from the images folder in the application directory and i have to display all the images on the page can any body tell me how to read all the images from the images folder which is in my appl开发者_开发知识库ication directory.
Thanks in Advance.
This could help
https://web.archive.org/web/20210304125318/https://www.4guysfromrolla.com/articles/052803-1.aspx
To add to the answer of Mr. Disappointment, you can get a list of all the files using the command specified,
To get the path of the application using Server.MapPath.
Then getting the list of files, you can simply iterate and filter on the extensions you need.
This will get you an array of the file names in a given path:
string[] fileNames = System.IO.Directory.GetFiles(yourPath);
You can then generate URLs for them and use <img>
tags written out to the response, for example something like ought to get the valid URLs:
string relativePath = Request.AppRelativeCurrentExecutionFilePath;
relativePath = relativePath.Substring(0, relativePath.LastIndexOf('/') + 1);
string requestPath = Path.GetDirectoryName(Server.MapPath(relativePath));
string[] fileNames = Directory.GetFiles(requestPath);
List<string> imageUrls = new List<string>(fileNames.Length);
foreach(var fileName in fileNames)
{
imageUrls.Add(Path.Combine(relativePath, Path.GetFileName(fileName)));
}
You could just write out the <img>
items in the loop. Also, note that a call to GetFiles
supplying only a path will return all available files so you might want to supply a searchPattern
argument such as *.jpg
.
精彩评论