System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象

时间:2023-03-08 19:28:54

做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")   获取路径本来这个方法用的好好的

因为需要实时的输出日志,我就使用了多线程,System.Web.HttpContext.Current.Server.MapPath()    他就报  未将对象引用设置为实例对象

在网上 找到 别人写的下面的方法 挺好用的

public static string MapPath(string strPath)
{
if (System.Web.HttpContext.Current != null)
{
return System.Web.HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
//strPath = strPath.Replace("/", "");
//strPath = strPath.Replace("~", "");
if (strPath.StartsWith("//"))
{
strPath = strPath.TrimStart('/');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}