Trying to access file on path – wwwroot/templates/file.txt. It works using -_hostingEnvironment.ContentRootPath + “templatesfile.txt” on windows but same path says file does not exists. What Am I missing
Advertisement
Answer
Trying to access file on path – wwwroot/templates/file.txt.
The following code snippet work for me, you can refer to it.
var filepath = Path.Combine(_hostingEnvironment.ContentRootPath, "templates", "file.txt");
var mes = "test message";
if (System.IO.File.Exists(filepath))
{
using (StreamReader file = new StreamReader(filepath))
{
mes = file.ReadLine();
}
}
ViewBag.fp = filepath;
ViewBag.mes = mes;
return View();
And please make sure the file is really existing under that folder on your server.
Test Result

