码迷,mamicode.com
首页 > 移动开发 > 详细

转:Server.MapPath相关

时间:2014-09-29 00:03:35      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   使用   ar   sp   art   on   c   

如果你从Page类继承的类中执行这条语句,才可以简单地使用 
DataBase = Server.MapPath("data.mdb");
否则写全命名空间:System.Web.HttpContext.Current.Server.MapPath();

总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径
1、Server.MapPath("/")
注:获得应用程序根目录所在的位置,如 C:\Inetpub\wwwroot\。
2、Server.MapPath("./")
注:获得所在页面的当前目录,等价于Server.MapPath("")。
3、Server.MapPath("../")
注:获得所在页面的上级目录。
4、Server.MapPath("~/")
注:获得当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置,如C:\Inetpub\wwwroot\Example\。

在多线程里面使用HttpContext.Current,HttpContext.Current是得到null的. 
所以在线程调用方法,方法中类里面的System.Web.HttpContext.Current.Server.MapPath() 获取不到对象。

应该这样用:

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

http://blog.csdn.net/lego2816/article/details/6781677

 

转:Server.MapPath相关

标签:blog   http   io   使用   ar   sp   art   on   c   

原文地址:http://www.cnblogs.com/ynzu/p/3998936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!