如何在asp.net核心中获取项目的根目录. Directory.GetCurrentDirectory()在Mac上似乎无法正常工作 [英] How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac

查看:87
本文介绍了如何在asp.net核心中获取项目的根目录. Directory.GetCurrentDirectory()在Mac上似乎无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的文件夹结构符合以下要求:

My project has a folder structure to the tune of:

  • 项目
  • 项目/数据
  • 项目/引擎
  • 项目/服务器
  • 项目/前端

在服务器中(在Project/Server文件夹中运行),我指的是这样的文件夹:

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);

在我的Windows计算机上,此代码工作正常,因为Directory.GetCurrentDirectory()已转至当前文件夹,并且可以执行

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); 

为我提供了项目的根文件夹(而不是bin或debug文件夹). 但是,当我在Mac上运行它时,它得到了"Directory.GetCurrentDirectory()"将我发送到/usr//[其他].它没有指向我的项目所在的文件夹.

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.

在我的项目中找到相对路径的正确方法是什么?我应该将数据文件夹存储在解决方案中的所有子项目都可以轻松访问的方式(特别是kestrel服务器项目)中的哪里?我宁愿不必将其存储在wwwroot文件夹中,因为数据文件夹是由团队中的其他成员维护的,而我只想访问最新版本.我有什么选择?

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?

推荐答案

取决于您在红est管道中的位置-如果您可以访问IConfiguration( Startup.cs构造函数)或IWebHostEnvironment(以前是IHostingEnvironment ),您可以将IWebHostEnvironment注入到构造函数中,也可以只从配置中请求密钥.

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.

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

在Startup.cs构造函数中使用IConfiguration

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

这篇关于如何在asp.net核心中获取项目的根目录. Directory.GetCurrentDirectory()在Mac上似乎无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆