System.Web.HttpContext.Current.Server.MapPath("/testtxt.txt").ToString()
//读出文件的物理路径
System.AppDomain.CurrentDomain.BaseDirectory.ToString() + ConfigurationManager.AppSettings["dbPath"]
//也是读出文件的物理路径(绝对路径+相对路径)!
在多线程里面使用HttpContext.Current,HttpContext.Current是得到null的.
这么用:
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "//");
if (strPath.StartsWith("//"))
{
//strPath = strPath.Substring(strPath.IndexOf('//', 1)).TrimStart('//');
strPath = strPath.TrimStart('//');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}