Search code examples
asp.nettilde-expansion

ASP.NET ~ Resolving


I have an asp.net application that has various text files it needs to load into a memory cache at start up. They are email body templates.

I have them as files in my project in Visual studio, (with File.ReadAllText) but when I try to load them it is looking in the development web server home directory rather than the application directory.

So I need to resolve ~ but I don't yet have an HttpRequest to do it with (since this is at application start up.) How can I get the absolute path of "~/StandardEmails/Sample.txt" during application startup?


Solution

  • Try HttpContext.Current.Server.MapPath("~/StandardEmails/Sample.txt").