2

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

必须有办法做到这一点。

4

1 回答 1

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
}
于 2009-07-19T14:00:18.727 回答