如果你從Page類繼承的類中執(zhí)行這條語句,才可以簡單地使用DataBase = Server.MapPath("data.mdb");否則寫全命名空間:System.Web.HttpContext.Current.Server.MapPath();
總注:Server.MapPath獲得的路徑都是服務(wù)器上的物理路徑,也就是常說的絕對路徑1、Server.MapPath("/")注:獲得應(yīng)用程序根目錄所在的位置,如 C:/Inetpub/wwwroot/。2、Server.MapPath("./")注:獲得所在頁面的當(dāng)前目錄,等價于Server.MapPath("")。3、Server.MapPath("../")注:獲得所在頁面的上級目錄。4、Server.MapPath("~/")注:獲得當(dāng)前應(yīng)用級程序的目錄,如果是根目錄,就是根目錄,如果是虛擬目錄,就是虛擬目錄所在的位置,如C:/Inetpub/wwwroot/Example/。
在多線程里面使用HttpContext.Current,HttpContext.Current是得到null的.所以在線程調(diào)用方法,方法中類里面的System.Web.HttpContext.Current.Server.MapPath() 獲取不到對象。
應(yīng)該這樣用:
public static string MapPath(string strPath) { if (HttpContext.Current != null) { return HttpContext.Current.Server.MapPath(strPath); } else //非web程序引用 { strPath = strPath.Replace("/", "http://"); if (strPath.StartsWith("http://")) { //strPath = strPath.Substring(strPath.IndexOf('//', 1)).TrimStart('//'); strPath = strPath.TrimStart('//'); } return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } }
http://blog.csdn.net/lego2816/article/details/6781677
新聞熱點(diǎn)
疑難解答