50

I have an ASP.NET Core (1.0-rc1-final) MVC solution and I wish to store a simple text file within the project which contain a list of strings which I read into a string array in my controller.

Where should I store this file in my project and how do I read those files in my controllers?

In ASP.net 4.x I'd have used the app_data folder and done something like this

string path = Server.MapPath("~/App_Data/File.txt");
string[] lines = System.IO.File.ReadAllLines(path);

But Server.MapPath does not seem to be valid in ASP.Net Core 1 and I'm not sure that the app_data folder is either.

6 Answers 6

34

I found a simple solution to this.

Firstly, you can create a folder anywhere in your solution, you do not have to stick to the conventions like 'app_data' from .net 4.x.

In my scenario, I created a folder called 'data' at the root of my project, I put my txt file in there and used this code to read the contents to a string array

var owners = System.IO.File.ReadAllLines(@"..\data\Owners.txt");

Sign up to request clarification or add additional context in comments.

3 Comments

This is not working for me in my machine, since it skips project folder. I. e.: it needs to be "D:\SOLUTION_FOLDER\PROJECT_FOLDER\data\Owners.txt" and it tries to read from "D:\SOLUTION_FOLDER\data\Owners.txt"
Your best choice is to always use IHostingEnvironment
From .NET Core 3.0 IHostingEnvironment is obslolete and should be replaced by IWebHostEnvironment
24

You can get the enviroment with Dependency Injection in your controller:

using Microsoft.AspNetCore.Hosting;
....

public class HomeController: Controller
{
   private IHostingEnvironment _env;

   public HomeController(IHostingEnvironment env)
   {
   _env = env;
   }   
...

Then you can get the wwwroot location in your actions: _env.WebRootPath

var owners =   System.IO.File.ReadAllLines(System.IO.Path.Combine(_env.WebRootPath,"File.txt"));

3 Comments

Why would _env.WebRootPath be null?
@Corpsekicker...you likely didn't register the dependency
Now IHostingEnvironment is obslolete and should be replaced by IWebHostEnvironment
16

This has always worked for me locally and on IIS.

AppDomain.CurrentDomain.BaseDirectory

To access your file, you could simply do the following:

import System.IO
...
var owners = File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File.txt"))

1 Comment

this also worked with me on Raspberry Pi working with dotnet core
14

in your controller you could take a dependency on IApplicationEnvironment and have it injected into the constructor then you can use it to establish the path to your file so your file can live in a folder within the project. In the example below "env" is the instance of IApplicationEnvironment

using Microsoft.Extensions.PlatformAbstractions;

var pathToFile = env.ApplicationBasePath 
   + Path.DirectorySeparatorChar.ToString() 
   + "yourfolder"
   + Path.DirectorySeparatorChar.ToString() 
   + "yourfilename.txt";

string fileContent;

using (StreamReader reader = File.OpenText(pathToFile))
{
    fileContent = reader.ReadToEnd();
}

ApplicationBasePath represents the applicationRootFolder

note that there also exists IHostingEnvironment which has the familiar .MapPath method, but it is for things stored below the wwwroot folder. You should only store things below the wwwroot folder that you want to serve with http requests so it is better to keep your list of strings in a different folder.

4 Comments

Thank you for your response, but I found a simpler method which I've posted and will mark as the answer. Thanks though
I gave a correct answer, just because it is not the exact syntax you chose in the end is not a great reason to not accept my answer imho. Also your answer if I'm not mistaken will only be correct on windows file system.
OK, I see your point that your solution will be cross platform and therefore better - have given you the answer, thanks
1. It looks like IApplicationEnvironment is now dead. IHostingEnvironment seems to have replaced it. 2. Path.Combine() will make this solution a lot more attractive: var pathToFile = System.IO.Path.Combine(env.ContentRootPath, "yourfolder", "yourfilename.txt");
10

IApplicationEnvironment and IRuntimeEnvironment have been removed as of an announcement on github on 2016/04/26.

I replaced @JoeAudette's code with this

private readonly string pathToFile;

public UsersController(IHostingEnvironment env)
{
    pathToFile = env.ContentRootPath
       + Path.DirectorySeparatorChar.ToString()
       + "Data"
       + Path.DirectorySeparatorChar.ToString()
       + "users.json";
}

Where my .json file is located at src/WebApplication/Data/users.json

I then read/parse this data like so

private async Task<IEnumerable<User>> GetDataSet()
{
    string source = "";

    using (StreamReader SourceReader = File.OpenText(pathToFile))
    {
        source = await SourceReader.ReadToEndAsync();
    }

    return await Task.FromResult(JsonConvert.DeserializeObject<IEnumerable<User>>(source)));
}

1 Comment

I guess that should be File.OpenText(pathToFile) not just OpenText(pathToFile)
6

This method worked for me locally and on Azure environment. It is taken from Joe's answer above

public static string ReadFile(string FileName)
    {
        try
        {
            using (StreamReader reader = File.OpenText(FileName))
            {
                string fileContent = reader.ReadToEnd();
                if (fileContent != null && fileContent != "")
                {
                    return fileContent;
                }
            }
        }
        catch (Exception ex)
        {
            //Log
            throw ex;
        }
        return null;
    }

And this is how invoked that method

string emailContent = ReadFile("./wwwroot/EmailTemplates/UpdateDetails.html");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.