相关疑难解决方法(0)

如何在asp.net核心中获取项目的根目录.Directory.GetCurrentDirectory()似乎无法在mac上正常工作

我的项目有一个文件夹结构:

  • 项目,
  • 项目/数据
  • 项目/发动机
  • 项目/服务器
  • 项目/前端

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

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);
Run Code Online (Sandbox Code Playgroud)

在我的Windows机器上,这个代码工作正常,因为它被Directory.GetCurrentDirectory()引用到当前文件夹,并且正在执行

rootFolder.Substring(0, rootFolder.IndexOf(@"\Project\", StringComparison.Ordinal) + @"\Project\".Length); 
Run Code Online (Sandbox Code Playgroud)

获取项目的根文件夹(不是bin或debug文件夹).但当我在Mac上运行它时,它" Directory.GetCurrentDirectory()"将我发送到/ usr // [其他东西].它没有引用我的项目所在的文件夹.

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

c# asp.net-core

42
推荐指数
9
解决办法
8万
查看次数

标签 统计

asp.net-core ×1

c# ×1