I am using .NET Core 3.1. Let's say that I have the following solution structure:
MySolution
ClassLibrary
Files
a.txt
b.txt
GetFile.cs
Project1
Project2
...
And let's say that GetFile.cs
has a function ReadFile
which reads the file from Files
and returns its content.
public class FileReaderService : IFileReaderService
{
private readonly IHostEnvironment _env;
public FileReaderService(IHostEnvironment env)
{
_env = env;
}
public string ReadFile(string fileName)
{
var currentPath = _env.ContentRootPath; // not correct
return "";
}
}
However, when I try to get the current directory in ReadFile
with _env.ContentRootPath
, it returns the directory of calling project. I don't want it to be dependent on calling project.
How can I achieve that each project will be able to call ReadFile
and that correct file from Files
will be returned? I need to be able to add, remove and change these files while the app is running.
I have found some questions on SO but they all seem to be outdated. I need a solution which will work on .NET Core.