1

当我尝试运行它时,在类字段上出现异常,如下所示:

非静态字段、方法或属性 'System.Web.UI.Page.MapPath(string) 需要对象引用

为什么 ?

后面的代码:

public partial class Profile : System.Web.UI.Page
{
    DirectoryInfo dir = new DirectoryInfo(MapPath("~/pic"));
4

3 回答 3

1

尝试Server.MapPath()HttpContext.Current.Server.MapPath()

于 2013-02-18T18:45:32.970 回答
1
DirectoryInfo dir = new DirectoryInfo(
HttpContext.Current.Request.PhysicalApplicationPath + "\\pic"));
于 2013-02-18T18:47:42.147 回答
0

MSDN文档:

...路径开头的斜杠 (/) 表示站点的绝对虚拟路径。

所以你的“~”是错误的和不必要的。

于 2013-02-18T18:41:58.753 回答