What are the differences between these 3 methods
Directory.GetCurrentDirectory()Environment.CurrentDirectoryhostingEnvironment.ContentRootPathused 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");
}
}
There is another option:
string TempPath4 = Path.Combine(hostingEnvironment.WebRootPath, "img", "Image1.png");
WebRootPath returns the path to the wwwroot folder.
This is recommended over using the first two options as they may not return the location that you want: Best way to get application folder path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With