添加引用 System.Web;
1.Context.Server.MapPath("/") 應用程序根目錄所在的位置 如 C:\Inetpub\wwwroot\
2.Context.Server.MapPath("./") 表示所在頁面的當前目錄
注:等價於Server.MapPath("") 返回 Server.MapPath("")所在頁面的物理文件路徑
3.Server.MapPath("../")表示上一級目錄
4.Server.MapPath("~/")表示當前應用級程序的目錄,如果是根目錄,就是根目錄,如果是虛擬目錄,就是虛擬目錄所在的位置 如:C:\Inetpub\wwwroot\Example\
注:等效於Server.MapPath("~")。
另:以下幾句等效
string filename=Server.MapPath("./") + @"\Web.config";
string filename=Server.MapPath("./") + "/Web.config";
string filename=Server.MapPath("") + @"\Web.config"
附一例子:修改web.config的某一節點的屬性值
public void write()
{
string key1 = this.TextBox1.Text;
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("")+"/web.config");
// 不是Tables[0]
ds.Tables[1].Rows[0][1] = key1;
ds.AcceptChanges();
ds.WriteXml(Server.MapPath("")+"/web.config");
ds.Dispose();
}
注:在類文件裏不能識別Server.MapPath("./") ,所以定義方法的時候放到其它類型的頁面裏了
參考文獻:http://haitaowang.blog.163.com/blog/static/128023119201129111841216/