What are the differences between these 3 methods
Directory.GetCurrentDirectory()
Environment.CurrentDirectory
hostingEnvironment.ContentRootPath
used to get the path to the image stored under wwwroot
? Seems like all work the same in my case, but would like to understand if there are any other case specific differences between them or benefits of using one over another.
I use this path to subsequently load the image into Bitmap MyBitmap
variable for further processing. Would like it to be environment resilient whatever it is finally deployed to Windows, Linux or container; locally or in the cloud.
Using Razor Pages with ASP.NET Core 3.0.
public class QRCodeModel : PageModel
{
private readonly IHostEnvironment hostingEnvironment;
public QRCodeModel(IHostEnvironment environment)
{
this.hostingEnvironment = environment;
}
public void OnGet()
{
string path1 = Path.Combine(Directory.GetCurrentDirectory(),
"wwwroot", "img", "Image1.png");
string path2 = Path.Combine(Environment.CurrentDirectory,
"wwwroot", "img", "Image1.png");
string path3 = Path.Combine(hostingEnvironment.ContentRootPath,
"wwwroot", "img", "Image1.png");
}
}