Reputation: 196489
In one of my controller actions I need to read in a text file that has a bunch of reference data in it. Right now I simply put it in the "/Content" directory.
My questions are:
Upvotes: 57
Views: 90116
Reputation: 32481
Ok this way it works for me (VS2017)
Use HostingEnvironment.MapPath(@"~/App_Data/file.txt")
(thanks to Hong comment)
var fileContents =
System.IO.File.ReadAllText(HostingEnvironment.MapPath(@"~/App_Data/file.txt"));
Upvotes: 23
Reputation: 31202
If the file should not be directly available via URL, you should put it in App_Data.
For reading it, just use:
var fileContents = System.IO.File.ReadAllText(Server.MapPath(@"~/App_Data/file.txt"));
Upvotes: 112