我正在尝试将“〜/ Uploads / Images /”转换为我可以创建FileStream的绝对路径。我已经尝试过VirtualPathUtility和Path.Combine,但似乎没有什么能给我正确的道路。我得到的最接近的是VirtualPathUtility.ToAppRelative,但这只是文件作为C:的直接子项的位置。
必须有办法做到这一点。
答案 0 :(得分:8)
您正在寻找MapPath
方法。
// get the path in the local file system that corresponds to ~/Uploads/Images
string localPath = HttpContext.Current.Server.MapPath("~/Uploads/Images/");
与Path.Combine一起使用以创建文件路径:
string fileName = Path.Combine(
HttpContext.Current.Server.MapPath("~/Uploads/Images/"),
"filename.ext");
using (FileStream stream = File.OpenRead(fileName))
{
// read the file
}