I'm trying to read a .txt file in my program:
using (StreamReader sr = new StreamReader(VirtualPathUtility.ToAbsolute("~/Content/txt/FamilyNames.txt")))
{
String line = sr.ReadToEnd();
Debug.WriteLine(line);
}
This however gives me following path, which is incorrect:
C:\\Content\\txt\\FamilyNames.txt
When I search for this I come up with numerous solutions like:
Server.MapPath();
But this seems to be outdated code? Because it doesn't get recognized in my Visual Studio, can't import it...
So what is the correct solution to get a path to a file in the content folder?
Server.MapPath needs a HTTPContext. Use System.Web.Hosting.HostingEnvironment.MapPath
instead.
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath("~/Content/txt/FamilyNames.txt")))
你有没有尝试过 :
StreamReader(VirtualPathUtility.ToAppRelative("~/Content/txt/FamilyNames.txt")))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.