Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of files from web URL outside my web application

Tags:

c#

.net

asp.net

I am using ASP.NET 4.5 with C#.

I have created a web API that points to the folders out of my web-application to get some shared resources. Now, I want to retrieve the list of files from the directory to which the web-api call points.

For example, my web application is resides on my local hard-drive's F:\Projects\Web1 directory. I have placed some images in C:\SharedRes\images directory. I have created a web-api that fetches images from this shared directory. For e.g. http://localhost/api/images/a.jpg will get the a.jpg from C:\SharedRes\images directory. It is working fine, but I want to get the list of files from C:\SharedRes\images whenever I make a call to web-api like http://localhost/api/images.

I have tried following :

DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("http://localhost/api/images"));
FileInfo[] file = directoryInfo.GetFiles().Where(f => f.Extension == (".bmp") || f.Extension == (".jpg") || f.Extension == (".png") || f.Extension == (".TIFF") || f.Extension == (".gif")).ToArray();

But it gives URI format not supported error.

Please let me know if any solution.

like image 587
Dev Avatar asked Oct 31 '25 11:10

Dev


1 Answers

HttpServerUtility.MapPath method gets virtual path as parameter.

You should use HttpContext.Current.Server.MapPath("/api/images")

like image 182
Volkan Vardar Avatar answered Nov 03 '25 00:11

Volkan Vardar