private void BtnReadWeb_Click(object sender, EventArgs e)
{
string webPath = System.Web.HttpContext.Current.Server.MapPath("") + @"\Web.config";
System.Console.ReadLine();
}
总是出现:未将对象引用设置到对象的实例
谢谢各位兄长,怎么解决!
{
string webPath = System.Web.HttpContext.Current.Server.MapPath("") + @"\Web.config";
System.Console.ReadLine();
}
总是出现:未将对象引用设置到对象的实例
谢谢各位兄长,怎么解决!
你的当前页面也许不在web根目录
?????????????????
console是控制台命令吧!
如果想在界面上显示,可以用:
Messagebox.show(webpath);
你在这句设个断点,逐步调试一下,看走到这步的时候string webPath的值是什麽,再检查一下等号后面是否写正确
注:获得应用程序根目录所在的位置,如 C:\Inetpub\wwwroot\。
2、Server.MapPath("./")
注:获得所在页面的当前目录,等价于Server.MapPath("")。
3、Server.MapPath("../")
注:获得所在页面的上级目录。
4、Server.MapPath("~/")
注:获得当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置
//string webPath = System.Web.HttpContext.Current.Server.MapPath("") + @"\Web.config";
string webPath = System.Web.HttpContext.Current.Server.MapPath("Web.config");