Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read a local file in my ASP.NET Core project?

If I want to read a local file from my project, given a relative path from the project directory, how could I achieve this?

like image 798
Muhammad Rehan Saeed Avatar asked Dec 14 '25 15:12

Muhammad Rehan Saeed


2 Answers

You can get the ApplicationBasePath from PlatformServices.ApplicationEnvironment. This will make it possible to resolve non-absolute paths relative to your application base path.

like image 125
Pawel Avatar answered Dec 16 '25 21:12

Pawel


Now that .NET Core has RTM-ed, you need to use IHostingEnvironment instead. It exposes WebRootPathand ContentRootPath.

like image 38
John Reilly Avatar answered Dec 16 '25 21:12

John Reilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!