2

I have a folder to hold images C:\inetpub\wwwroot\Images\eZone\Albums\Album1. My mvc app is in another folder C:\inetpub\wwwroot\ezone. In IIS 7, I create a virtual directory images, which maps to C:\inetpub\wwwroot\images, under ezone site. When I run my ezone app to load images from album1

DirectoryInfo di = new DirectoryInfo(Server.MapPath("/images/ezone/albums/album1"));

it returns "C:\inetput\wwwroot\ezone\images\ezone\albums\album1" which is not correct. What am I missing?

T L
  • 504
  • 2
  • 11
  • 27
  • It seems odd that its stripping out the 'ezone' from the MapPath string. I would thing the return value would be "C:\inetpub\wwwroot\ezone\images\ezone\albums\album1". Notice the 2nd ezone – Mark Olsen Jun 30 '15 at 19:48
  • Also, is there a reason you have the images outside of the web app folder? – Mark Olsen Jun 30 '15 at 19:51
  • Mark, actually, our administrators recommended us to leave the images folder on a separate drive for security reason. I was trying to see if I can even get it to work on a same drive. – T L Jun 30 '15 at 20:50
  • What if you don't use Server.MapPath and just do DirectoryInfo(fullPath). Does that work? – Mark Olsen Jun 30 '15 at 21:07
  • It assumes the folder is at the root C:\ so my next line of code trying to retrieve files from the folder throws the exception "could not find a part of the path 'C:\Images\ezone\albums\album1'." – T L Jun 30 '15 at 21:31
  • I mean just use the full "C:\inetpub\wwwroot\Images\eZone\Albums\Album1". I don't think Server.MapPath is going to help you with this. I think your best bet might be to have an appsetting with the fully qualified path that could contain could have different setting for dev and prod – Mark Olsen Jun 30 '15 at 22:01
  • Possible duplicate of [How to get virtual directory physical path](http://stackoverflow.com/questions/10986525/how-to-get-virtual-directory-physical-path) – needfulthing Sep 09 '16 at 14:10

1 Answers1

2

Put a tilde in front of your path:

var di = new DirectoryInfo(Server.MapPath("~/images/ezone/albums/album1"));

For more information, have a look at ASP.NET MapPath resolves Virtual, Physical Paths

Jens R.
  • 191
  • 5