Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我尝试运行它时,在类字段上出现异常,如下所示:
非静态字段、方法或属性 'System.Web.UI.Page.MapPath(string) 需要对象引用
为什么 ?
后面的代码:
public partial class Profile : System.Web.UI.Page { DirectoryInfo dir = new DirectoryInfo(MapPath("~/pic"));
尝试Server.MapPath()或HttpContext.Current.Server.MapPath()
Server.MapPath()
HttpContext.Current.Server.MapPath()
DirectoryInfo dir = new DirectoryInfo( HttpContext.Current.Request.PhysicalApplicationPath + "\\pic"));
从MSDN文档:
...路径开头的斜杠 (/) 表示站点的绝对虚拟路径。
所以你的“~”是错误的和不必要的。