C# Server.MapPath 未将对象引用设置到对象的实例

来源:互联网 发布:android 监听数据变化 编辑:程序博客网 时间:2024/06/08 05:38

System.Web.HttpContext.Current.Server.MapPath(string sfilePath)将虚拟路径转换成物理路径。这个必须在aspx或者MVC中Action调用才行,即必须是有HttpContext.Current对象。在线程执行任务中若是调用了System.Web.HttpContext.Current.Server.MapPath(string sfilePath)也会报异常,因为没有HttpContext.Current对象。

解决方法:使用System.AppDomain.CurrentDomain.BaseDirectory(应用程序根路径)拼接路径