Read solution data files ASP.Net Core

Viewed 72998

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

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"))
Related