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

My project has a folder structure:

  • Project,
  • Project / Data
  • Project / Engine
  • Project / Server
  • project / front end

On the server (running in the Project / Server folder), I refer to the folder as follows:

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 computer, this code works fine, because Directory.GetCurrentDirectory() added to the current folder and does

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

gets the project root folder (not bin or debug folders). But when I ran it on mac, it got " Directory.GetCurrentDirectory() " sent me to / usr // [something else]. This does not apply to the folder where my project is located.

What is the correct way to find relative paths in my project? Where should I store the data folder so that it is easily accessible for all subprojects in the solution - in particular, for the kestrel server project? I prefer not to store it in the wwwroot folder, because the data folder is maintained by another member on the team, and I just want to access the latest version. What are my options?

+37
source share
7 answers

Depending on where you are in the kestrel pipeline - if you have access to IConfiguration ( Startup.cs constructor) or IHostingEnvironment you can either enter IHostingEnvironment into the constructor or simply request a configuration key.

Introduce IHostingEnvironment in Startup.cs Constructor

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

Using IConfiguration in Startup.cs Constructor

 public Startup(IConfiguration configuration) { var contentRoot = configuration.GetValue<string>(WebHostDefaults.ContentRootKey); } 
+35
source

Powered by .Net Core 2.2 and 3.0 at the moment.

To get the root directory of projects in the controller:

  • Create a property for the placement 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 root path of the projects

     string projectRootPath = _hostingEnvironment.ContentRootPath; 

To get the path " wwwroot ", use

 _hostingEnvironment.WebRootPath 
+34
source

As previously answered (and removed). To get the base directory, as in the location of the running assembly, do not use Directory.GetCurrentDirectory (), rather get it from IHostingEnvironment.ContentRootPath.

 private IHostingEnvironment _hostingEnvironment; private string projectRootFolder; public Program(IHostingEnvironment env) { _hostingEnvironment = env; projectRootFolder = env.ContentRootPath.Substring(0, env.ContentRootPath.LastIndexOf(@"\ProjectRoot\", StringComparison.Ordinal) + @"\ProjectRoot\".Length); } 

However, I made an additional error: I set the ContentRoot Directory to Directory.GetCurrentDirectory () at startup, undermining the default value that I wanted so much! Here I commented on an offensive line:

  public static void Main(string[] args) { var host = new WebHostBuilder().UseKestrel() // .UseContentRoot(Directory.GetCurrentDirectory()) //<== The mistake .UseIISIntegration() .UseStartup<Program>() .Build(); host.Run(); } 

Now it works correctly - now I can go to the subfolders of my project with:

 var pathToData = Path.GetFullPath(Path.Combine(projectRootFolder, "data")); 

I realized my mistake by reading BaseDirectory vs. The Current Directory and @CodeNotFound found the answer (which was removed because it did not work due to the above error), which can mainly be found here: Getting the path and path to the WebRoot root directory in Core Asp.net

+17
source

In some cases, _hostingEnvironment.ContentRootPath and System.IO.Directory.GetCurrentDirectory() oriented to the source directory. Here is a mistake about it.

The solution proposed there helped me

 Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); 
+13
source

Try to find here: The best way to get the path to the application folder

To quote from there:

System.IO.Directory.GetCurrentDirectory() returns the current directory, which may or may not be the folder in which the application is located. The same goes for Environment.CurrentDirectory. If you use this in a DLL file, it will return the path where the process is running (this is especially true in ASP.NET).

+9
source

I solved the problem with this code:

 using System.IO; var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\"))) 
+1
source

If you are using ASP.NET MVC Core 3 or later, IHostingEnvironment deprecated and replaced with IWebHostEnvironment

 public Startup(IWebHostEnvironment webHostEnvironment) { var webRootPath = webHostEnvironment.WebRootPath; } 
0
source

Source: https://habr.com/ru/post/1267332/


All Articles