在开发中经常用到应用程序的物理路径,在获取应用程序中文件的物理路径时最常用:

  string path = Server.MapPath("Document/example.doc");

  这里用Server.MapPath得到应用程序的物理路径。如果你是在当前 web 项目下的一个类中如上写链接语句,可能会提示找不到Server,因为 Server的完整路径是System.Web.HttpContext.Current.Server。using System.Web是显然不够的,但是在从内置 Page 类继承的类中可以直接写Server.MapPath("Document/example.doc"),应该是因为 Page 类已经包含了这些类路径。
  如果你从 Page 类继承的类中执行这条语句,才可以简单地使用

  string path= Server.MapPath("Document/example.doc");
  否则写全命名空间:string path = System.Web.HttpContext.Current.Server.MapPath("Document/example.doc");
  

  注意:如果是在一个类库下的一个类中、要先添加引入using System.Web,因为新建一个类库时默认不引入 using System.Web。

相关文章:

  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2021-05-24
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
相关资源
相似解决方案