ASP.NET:Path.Combine相对路径
-
18-09-2019 - |
题
我想“〜/上传/图片/”转换为绝对路径,我可以创建一个FileStream。我试过VirtualPathUtility和Path.Combine,但似乎没有给我正确的道路。我得到的最接近是VirtualPathUtility.ToAppRelative,但是这仅仅是文件的为C的直接子位置:
必须有一种方法来做到这一点。
解决方案
您正在寻找 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
}
不隶属于 StackOverflow