gpt4 book ai didi

ASP.NET:Path.Combine 与相对路径

转载 作者:行者123 更新时间:2023-12-02 18:01:43 24 4
gpt4 key购买 nike

我正在尝试将“~/Uploads/Images/”转换为可以从中创建文件流的绝对路径。我尝试过 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
}

关于ASP.NET:Path.Combine 与相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1149949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com