日期:2014-05-17  浏览次数:20469 次

问一下各位大神,关于获得当前请求的绝对路径问题
我最近在看dtcms系统,其中有段程序是获得当前绝对路径,前面都能理解,只是后半段蓝色的说非web程序引用,那一段代码很多“\\”是什么意思,非web 程序引用是指什么,最好能稍微举个例子说明下非web程序引用,谢谢

/// 获得当前绝对路径
        /// </summary>
        /// <param name="strPath">指定的路径</param>
        /// <returns>绝对路径</returns>
        public static string GetMapPath(string strPath)
        {
            if (strPath.ToLower().StartsWith("http://"))
            {
                return 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('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }

        }

------解决方案--------------------
这个方法所在的地方应该是独立的dll,它可能被web程序引用,也可能被非web程序引用,如果是非web程序(比如控制台程序)引用,那这个值HttpContext.Current就是空,就不能用Server.MapPath方法求映射路径,所以后边是有用的,在非web方式访问本地资源时用。。

(其实呢,今天瞭眼一看差26分10000,到10000就能升级了,然后收手,N久没回过技术帖了,生分了,诶老了妈的)