在我写的c#类中,我有一个照片属性,如果图像存在则返回照片源(否则为默认图像).在我的代码中我使用:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我得到此图像的FileInfo()信息,我看到我试图在以下目录中找到此图像:C:\ Program Files(x86)\ Common Files\Microsoft Shared\DevServer\10.0 \〜\ images\recipes
当然图像不在该目录中,File.Exists返回错误的值.但我该如何解决这个问题呢?
s_h*_*itt 11
试试这个:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
Run Code Online (Sandbox Code Playgroud)