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

 

 

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

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

 是因为System.Web.HttpContext.Current.Server.MapPath() 是web程序的写法,非web程序请参考下面

  

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);
    }
}    

 

posted @ 2015-09-18 14:26  秋香姑娘请你不要紧张  阅读(1588)  评论(0编辑  收藏  举报