How to dynamically grab the directory from the Content folder?

1

My question is the same as the title: how do I create a variable that points to the Content folder?

I have a file there and I want to access it. I tried this way, but without success:

StreamReader file =
new StreamReader(@"~/Content/tb_ocupacao.txt", Encoding.GetEncoding("iso-8859-1"));
    
asked by anonymous 23.06.2017 / 00:28

1 answer

4

Use Server.MapPath

If you will use the code in a controller

var caminho = Server.MapPath("~/Content");
var file = new StreamReader($"{caminho}/tb_ocupacao.txt", 
                              Encoding.GetEncoding("iso-8859-1"));

If it will be used outside

var caminho = System.Web.HttpContext.Current.Server.MapPath("~/Content");
var file = new StreamReader($"{caminho}/tb_ocupacao.txt", 
                              Encoding.GetEncoding("iso-8859-1"));
    
23.06.2017 / 00:39