24

I have an image in wwwroot/img folder and want to use it in my server side code.

How can I get the path to this image in code?

The code is like this:

Graphics graphics = Graphics.FromImage(path)
Lukas
  • 1,699
  • 1
  • 16
  • 49
Techy
  • 2,026
  • 4
  • 20
  • 41

5 Answers5

32

It would be cleaner to inject an IHostingEnvironment and then either use its WebRootPath or WebRootFileProvider properties.

For example in a controller:

private readonly IHostingEnvironment env;
public HomeController(IHostingEnvironment env)
{
    this.env = env;
}

public IActionResult About(Guid foo)
{
    var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath
}

In a view you typically want to use Url.Content("images/foo.png") to get the url for that particular file. However if you need to access the physical path for some reason then you could follow the same approach:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment env
@{ 
 var path = env.WebRootFileProvider.GetFileInfo("images/foo.png")?.PhysicalPath
}
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112
  • 4
    With a new version of .NET IHostingEnvironment is deprecated and replaced by IWebHostEnvironment but the code here will look the same and work the same :) – Hawlett Jun 16 '20 at 15:18
6

Building on Daniel's answer, but specifically for ASP.Net Core 2.2:

Use dependency injection in your controller:

[Route("api/[controller]")]
public class GalleryController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;
    public GalleryController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }        

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public IActionResult Get(int id)
    {
        var path = Path.Combine(_hostingEnvironment.WebRootPath, "images", $"{id}.jpg");
        var imageFileStream = System.IO.File.OpenRead(path);
        return File(imageFileStream, "image/jpeg");
    }
}

A concrete instance of the IHostingEnvironment is injected into your controller, and you can use it to access WebRootPath (wwwroot).

Scott Ferguson
  • 7,690
  • 7
  • 41
  • 64
4

FYI.Just an update to this. In ASP.NET Core 3 & Net 5 it's the following:

    private readonly IWebHostEnvironment _env;

    public HomeController(IWebHostEnvironment env)
    {
        _env = env;

    }

    public IActionResult About()
    {
      var path = _env.WebRootPath;
    }
L. Piotrowski
  • 1,533
  • 1
  • 20
  • 35
2

This works:

private readonly IHostingEnvironment env;
public HomeController(IHostingEnvironment env)
{
    this.env = env;
}

public IActionResult About()
{
    var stream = env.WebRootFileProvider.GetFileInfo("image/foo.png").CreateReadStream();
    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
    Graphics graphics = Graphics.FromImage(image);
}
A. Morel
  • 9,210
  • 4
  • 56
  • 45
-7
 string path =  $"{Directory.GetCurrentDirectory()}{@"\wwwroot\images"}";
ravindra
  • 322
  • 3
  • 14
  • 4
    This won't work if you serve your static files from a different folder. Even worse, `Directory.GetCurrentDirectory` might [not return what you expect](http://stackoverflow.com/questions/10951599/getting-current-directory-in-net-web-application) when deployed to a server like IIS. – Daniel J.G. Mar 04 '17 at 17:36
  • 1
    This will fail on Azure too. – Scott Ferguson Mar 13 '19 at 08:46