测试有效的 :  系统的根目录
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;
}
将虚拟路径转绝对路就没什么好说的了

asp.net 获取系统的根目录的更多相关文章

  1. File类_常见的方法(获取系统根目录与指定目录的容量)

    获取系统根目录 import java.io.File; public class File_ListRoots { public static void main(String[] args) { ...

  2. ASP.NET路由系统实现原理:HttpHandler的动态映射

    我们知道一个请求最终通过一个具体的HttpHandler进行处理,而我们熟悉的用于表示一个Web页面的Page对象就是一个HttpHandler,被用于处理基于某个.aspx文件的请求.我们可以通过H ...

  3. ASP.NET获取路径的方法

    原文:[转载]ASP.NET获取路径的方法 HttpContext.Current.Request.PhysicalPath;    // 获得当前页面的完整物理路径.比如 F:\XFU.NSQS\p ...

  4. 【转载】ASP.NET获取路径的方法

    HttpContext.Current.Request.PhysicalPath;    // 获得当前页面的完整物理路径.比如 F:\XFU.NSQS\project\website\Default ...

  5. ASP.NET获取客户端、服务器端的信息

    ASP.NET获取客户端.服务器端基础信息 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名: ...

  6. ThinPHP命名空间,连接数据库是要修改的配置文件,Model数据模型层,跨控制器调用,如何获取系统常量信息,

    一.命名空间(主要是为了实现自动加载类) *命名空间(相当于虚拟的目录),为了让类有一个统一的文件夹来管理(可以自动加载'类'),每个文件都要有命名空间*tp如何做命名空间:*TP框架下有一个初始命名 ...

  7. c# 如何获取项目的根目录

    c# 如何获取项目的根目录 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设 ...

  8. 【转】cocos2d-x获取系统时间——2013-08-25 10

    欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...

  9. C# ,asp.net 获取当前,相对,绝对路径(转)

    C# ,asp.net 获取当前,相对,绝对路径 一.C#获取当前路径的方法: . System.Diagnostics.Process.GetCurrentProcess().MainModule. ...

随机推荐

  1. hdu 5108

    //题意是给一个数N,然后让你求M,使得N/M为素数,并且M的值最小 //思路呢,大概有两种,一个是遍历素数求解的,不过数据太大不现实 //另外一种就是质因数求解,for循环是遍历质因数,然后whil ...

  2. 关于webstorm(phpstorm)设置了编码格式之后还是乱码的问题

    今天在使用phpstorm的时候,页面开始是设置utf-8的,一切正常.但是,当我从一个gbk页面复制了一段代码到phpstorm里面的时候,页面预览的时候,居然打不开了,显示是乱码.接着我就把复制的 ...

  3. fastUtils学习

    比传统java集合工具类速度更快 google的guava也新增了java容器新的功能,功能更加强大,参考文档:http://www.ibm.com/developerworks/cn/java/j- ...

  4. Android中滑动关闭Activity

    继承SwipeBackActivity即可实现向右滑动删除Activity效果 点击下载所需文件

  5. ESXi控制台TSM:弥补vSphere Client不足

    当vSphere Client不能完成某些任务时,主机的ESXi控制台及其技术支持模式(TSM)可能能派上用场. ESXi控制台允许管理员执行不能通过vSphere Client进行配置的管理任务,比 ...

  6. Swift中出现“no such module cocoa”的错误

    在Swift开发中,新建了一个UIViewController的子类出现“No such module 'Cocoa' 的错误, 头部是import cocoa.. 原因很简单:在建立新的File文件 ...

  7. Oracle数据库之序列

    Oracle数据库之序列(sequence) 序列是一个计数器,它并不会与特定的表关联.我们可以通过创建Oracle序列和触发器实现表的主键自增.序列的用途一般用来填充主键和计数. 一.创建序列 语法 ...

  8. Backbone学习笔记

    model model的get和set是对model.attributes进行操作,并不是直接对model进行操作 collection collection.set()会触发相应的add,remov ...

  9. 转载:Java连接MySQL 数据库的正确操作流程

    转载网址:http://www.bitscn.com/pdb/mysql/201005/186551.html       以下的文章主要介绍的是Java连接MySQL 数据库(以MySQL数据库为例 ...

  10. 设置CentOS里的Mysql开启客户端远程连接

    CentOS系统安装好MySQL后,默认情况下不支持用户通过非本机连接上数据库服务器,下面是解决方法: 1.在控制台执行 mysql -u root -p mysql,系统提示输入数据库root用户的 ...