Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac

My project has a folder structure to the tune of:

  • Project,
  • Project/data
  • Project/Engine
  • Project/Server
  • project/front-end

In the server (running in the Project/Server folder) I refer to the folder like this:

var rootFolder = Directory.GetCurrentDirectory(); rootFolder = rootFolder.Substring(0,             rootFolder.IndexOf(@"\Project\", StringComparison.Ordinal) + @"\Project\".Length); PathToData = Path.GetFullPath(Path.Combine(rootFolder, "Data"));  var Parser = Parser(); var d = new FileStream(Path.Combine(PathToData, $"{dataFileName}.txt"), FileMode.Open); var fs = new StreamReader(d, Encoding.UTF8); 

On my windows machine this code works fine since Directory.GetCurrentDirectory() reffered to the current folder, and doing

rootFolder.Substring(0, rootFolder.IndexOf(@"\Project\", StringComparison.Ordinal) + @"\Project\".Length);  

gets me the root folder of the project (not the bin or debug folders). But when I ran it on a mac it got "Directory.GetCurrentDirectory()" sent me to /usr//[something else]. It didn't refer to the folder where my project lies.

What is the correct way to find relative paths in my project? Where should I store the data folder in a way that it is easily accessible to all the sub projects in the solution - specifically to the kestrel server project? I prefer to not have to store it in the wwwroot folder because the data folder is maintained by a different member in the team, and I just want to access the latest version. What are my options?

like image 362
akraines Avatar asked Apr 30 '17 18:04

akraines


People also ask

How do I get the root path in .NET core?

The Application Base (Root) path is available in the ContentRootPath property of the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .

What is root folder in ASP.Net Core?

By default, the wwwroot folder in the ASP.NET Core project is treated as a web root folder. Static files can be stored in any folder under the web root and accessed with a relative path to that root.

Where is the project root directory?

The root directory, or root folder, is the top-level directory of a file system. The directory structure can be visually represented as an upside-down tree, so the term "root" represents the top level. All other directories within a volume are "branches" or subdirectories of the root directory.


2 Answers

Depending on where you are in the kestrel pipeline - if you have access to IConfiguration (Startup.cs constructor) or IWebHostEnvironment (formerly IHostingEnvironment) you can either inject the IWebHostEnvironment into your constructor or just request the key from the configuration.

Inject IWebHostEnvironment in Startup.cs Constructor

public Startup(IConfiguration configuration, IWebHostEnvironment env) {      var contentRoot = env.ContentRootPath; } 

Using IConfiguration in Startup.cs Constructor

public Startup(IConfiguration configuration) {      var contentRoot = configuration.GetValue<string>(WebHostDefaults.ContentRootKey); } 
like image 187
SliverNinja - MSFT Avatar answered Sep 23 '22 10:09

SliverNinja - MSFT


Working on .Net Core 2.2 and 3.0 as of now.

To get the projects root directory within a Controller:

  • Create a property for the hosting environment

    private readonly IHostingEnvironment _hostingEnvironment; 
  • Add Microsoft.AspNetCore.Hosting to your controller

    using Microsoft.AspNetCore.Hosting; 
  • Register the service in the constructor

    public HomeController(IHostingEnvironment hostingEnvironment) {     _hostingEnvironment = hostingEnvironment; } 
  • Now, to get the projects root path

    string projectRootPath = _hostingEnvironment.ContentRootPath; 

To get the "wwwroot" path, use

_hostingEnvironment.WebRootPath 
like image 32
StefanJM Avatar answered Sep 23 '22 10:09

StefanJM