zoukankan      html  css  js  c++  java
  • asp.net 获取系统的根目录

    测试有效的 :  系统的根目录
    HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).ToLower();           

    以下为参考:

    ASP.NET 根路径的获取
            private string _ApplicationPath;   
            /// <summary>   
            /// 虚拟应用程序根路径   
            /// </summary>   
            public string ApplicationPath   
            {   
                get  
                {   
                    _ApplicationPath = HttpContext.Current.Request.ApplicationPath;   
                    if (_ApplicationPath.Length == 1)   
                    {   
                        _ApplicationPath = "";   
                    }   
                    return _ApplicationPath;   
                }   
            }   
            private string _CurrentPath;   
            /// <summary>   
            /// 当前的绝对路径   
            /// </summary>   
            public string CurrentPath   
            {   
                get  
                {   
                    _CurrentPath = HttpContext.Current.Server.MapPath(".").ToLower();//当前的绝对路径(这里MapPath里的"."代表当前服务器)   
                       
                    if (_CurrentPath.Length == 1)   
                    {   
                        _CurrentPath = "";   
                    }   
                    return _CurrentPath;   
                }   
            }   
            private string _RootPath;   
            /// <summary>   
            /// 系统的根目录   
            /// </summary>   
            public string RootPath   
            {   
                get  
                {   
      
                    _RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).ToLower();//当前的绝对路径              
                    if (_RootPath.Length == 1)   
                    {   
                        _RootPath = "";   
                    }   
                    return _RootPath;   
                }   
            }

    Web站点下的绝对路径转换为相对于指定页面的虚拟路径
    第一个方法
    /**//// <summary>
    ///
    Web站点下的绝对路径转换为相对于指定页面的虚拟路径
    /// </summary>
    /// <param name="page">
    当前页面指针,一般为this</param>
    /// <param name="specifiedPath">
    绝对路径</param>
    /// <returns>
    虚拟路径型如: http://www.cnblogs.com/</returns>
    public static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
    {
        // 
    根目录虚拟路径
        string virtualPath = page.Request.ApplicationPath;
        // 
    根目录绝对路径
        string pathRooted = HostingEnvironment.MapPath(virtualPath);
        // 
    页面虚拟路径
        string pageVirtualPath = page.Request.Path;
        if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
        {
            throw new Exception(string.Format(""{0}"
    是虚拟路径而不是绝对路径!", specifiedPath));
        }
        // 转换成相对路径 
        //(
    测试发现,pathRooted  VS2005 自带的服务器跟在IIS下根目录或者虚拟目录运行似乎不一样,
        // 
    有此地方后面会加"", 有些则不会, 为保险起见判断一下)
        if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\")
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "/");
        }
        else
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "");
        }
        string relativePath = specifiedPath.Replace("\", "/");
        string[] pageNodes = pageVirtualPath.Split('/');
        // 减去最后一个页面和前面一个 "" 
        int pageNodesCount = pageNodes.Length - 2;
        for (int i = 0; i < pageNodesCount; i  )
        {
            relativePath = "/.."   relativePath;
        }
        if (pageNodesCount > 0)
        {
            // 
    如果存在 ".." , 则把最前面的 "/" 去掉
            relativePath = relativePath.Substring(1, relativePath.Length - 1);
        }
        return relativePath;
    }
     
     
    第二个方法
    Web站点下的绝对路径转换为虚拟路径
    /**//// <summary>
    /// 
    Web站点下的绝对路径转换为虚拟路径
    /// 
    注:非Web站点下的则不转换
    /// </summary>
    /// <param name="page">
    当前页面指针,一般为this</param>
    /// <param name="specifiedPath">
    绝对路径</param>
    /// <returns>
    虚拟路径型如: ~/</returns>
    public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)
    {
        string virtualPath = page.Request.ApplicationPath;
        string pathRooted = HostingEnvironment.MapPath(virtualPath);
        if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
        {
            return specifiedPath;
        }
        if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\")
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "~/");
        }
        else
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "~");
        }
        string relativePath = specifiedPath.Replace("\", "/");
        return relativePath;
    }
    将虚拟路径转绝对路就没什么好说的了
  • 相关阅读:
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第50章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第49章 读书笔记(待更新)
    Algebra, Topology, Differential Calculus, and Optimization Theory For Computer Science and Machine Learning 第48章 读书笔记(待更新)
    Spring Boot 中使用 Quartz 实现任务调度
    实战 FastDFS Java 客户端上传文件
    分布式文件系统之 FastDFS
    Java 持久层框架之 MyBatis
    C语言实现贪吃蛇
    [转载]分享三篇非常好的学习心得
    selenium加载cookie报错问题:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3246996.html
Copyright © 2011-2022 走看看