Search code examples
c#asp.net-mvcrelative-pathsystem.io.file

image.fromfile() cannont use relative path "~/folder/file"


Greeting people,

I'm having a bit of trouble with loading images from file. When i want to use my files anywhere else i use a relative path like "~/folder/file" but when i try to create an image using that method it cannot be found. Since i'm not willing to use absolute paths and change the accordingly after every publish does anyone know how to set the relative path in this case?

These particulare jpgs are in the Vouchers/ directory in the project's root but cannot be found through "~/Vouchers/spring.jpg" for example


Solution

  • In order to avoid hardcoding absolute paths but still be able to provide absolute paths via relative paths, just let the system put them together for you one the fly:

    System.Web.Hosting.HostingEnvironment.MapPath("~/Vouchers/spring.jpg")