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 に答える