当我尝试运行此给我类字段例外如下所示:为MapPath的字段对象参考
一个对象引用是所必需的非静态字段,方法或 属性“的System.Web .UI.Page.MapPath(string)
为什么?
后面的代码:
public partial class Profile : System.Web.UI.Page
{
DirectoryInfo dir = new DirectoryInfo(MapPath("~/pic"));
当我尝试运行此给我类字段例外如下所示:为MapPath的字段对象参考
一个对象引用是所必需的非静态字段,方法或 属性“的System.Web .UI.Page.MapPath(string)
为什么?
后面的代码:
public partial class Profile : System.Web.UI.Page
{
DirectoryInfo dir = new DirectoryInfo(MapPath("~/pic"));
DirectoryInfo dir = new DirectoryInfo(
HttpContext.Current.Request.PhysicalApplicationPath + "\\pic"));
尝试Server.MapPath()
或HttpContext.Current.Server.MapPath()
现在工作。你能解释一下为什么,问题的原因是什么? – TheChampp 2013-02-18 18:49:52
@Win或'@“\ pic”' – DiskJunky 2013-02-18 18:50:12
正如DiskJunky所说,您也可以使用'at'符号。 TheCampp - 它基本上返回您的网站的实际文件夹内的图片文件夹。例如,c:\ wwwroot \ yoursite \ pic – Win 2013-02-18 18:52:23