ASP.NET:具有相对路径的Path.Combine

Ech*_*lon 2 asp.net relative-path absolute-path

我正在尝试将"〜/ Uploads/Images /"转换为我可以创建FileStream的绝对路径.我尝试过VirtualPathUtility和Path.Combine,但似乎没有什么能给我正确的道路.我得到的最接近的是VirtualPathUtility.ToAppRelative,但这只是文件作为C:的直接子项的位置.

必须有办法做到这一点.

Fre*_*örk 8

您正在寻找该MapPath方法.

// get the path in the local file system that corresponds to ~/Uploads/Images
string localPath = HttpContext.Current.Server.MapPath("~/Uploads/Images/");
Run Code Online (Sandbox Code Playgroud)

与Path.Combine一起使用以创建文件路径:

string fileName = Path.Combine(
                      HttpContext.Current.Server.MapPath("~/Uploads/Images/"),
                      "filename.ext");
using (FileStream stream = File.OpenRead(fileName))
{
   // read the file
}
Run Code Online (Sandbox Code Playgroud)