一般加载xml路径,我们都会用
string Path =System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Config.xml") 
XmlDocument xd = new XmlDocument();
 xd.Load(Path);
以上代码可以正常执行.
但在Application_Start,用到Current对象时(如Response,Server),会报未将对象引用设置到对象的实例。
现在只能退一步不用Current.
遇到问题是取加载路径,
用System.Web.HttpRuntime.AppDomainAppVirtualPath + "/App_Data/Config.xml"去替换,执行时间报错找不到文件。
如何解决,难道不用Server.MapPath,就读不到web项目文件?求解。。
另外若能处理Application_Start中 HttpContext.Current对象不能为空方法也行。

解决方案 »

  1.   

    var path = Sytem.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Config.xml");
      

  2.   

    程序还没有启动,当前没有Current了。
    用这个 HttpRuntime.AppDomainAppPath
      

  3.   

    1与2都正解.以下三种得到路径一样。 Response.Write(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Config.xml") + "<br/>");
            Response.Write(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data\\Config.xml")+"<br/>");
            Response.Write(System.Web.HttpRuntime.AppDomainAppPath + "App_Data\\Config.xml" + "<br/>");