测试有效的 :  系统的根目录
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. ZOJ1524

    题意:给定需要购买物品的顺序以及总物品对应的价格,求解按顺序购买物品时最小花费. 输入: m,n(m代表需要购买物品的清单,n代表总的物品数) Xi...(代表对应物品的序号以及价格) 输出: cos ...

  2. apache也可以做负载均衡,跟nignx的区别是什么?

    后续更新中.. 参考 http://zhumeng8337797.blog.163.com/blog/static/100768914201242211633248/ 比较 http://zhan.r ...

  3. activiti总结2

    根据流程号查询失败原因. activiti重试机制.齿轮节点.邮件节点.任务节点.ACT_HI_ACTINST历史表.ACT_RU_EXECUTION运行表.看图. 在Eclipse里面自己写个测试方 ...

  4. Android 软件盘 动态设置 layout

    总体来说分为三种方法: 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: ...

  5. Nginx配置域名跳转实例

    要求:浏览器地址栏输入qj.123.com之后,地址自动变成qj.abc.com 配置nginx跳转 server { listen 80; server_name qj.abc.com qj.123 ...

  6. uva 12207 - That is Your Queue

    #include <cstdio> #include <iostream> #include <deque> using namespace std; int ma ...

  7. Oracle数据库之PL/SQL异常处理

    Oracle数据库之PL/SQL异常处理 异常指的是在程序运行过程中发生的异常事件,通常是由硬件问题或者程序设计问题所导致的. PL/SQL程序设计过程中,即使是写得最好的程序也可能会遇到错误或未预料 ...

  8. MySQL递归查询所有子节点,树形结构查询

    --表结构 CREATE TABLE `address` ( `id` int(11) NOT NULL AUTO_INCREMENT, `code_value` varchar(32) DEFAUL ...

  9. IIS7.5 APPCMD 简单用法及示例

    1 添加应用程序进城池 appcmd.exe add apppool  /name:test.com  /managedRuntimeVersion:"v4.0" /managed ...

  10. Matlab与外部接口:MAT文件基础

    MAT 文件MAT文件是MATLAB使用的一种特有的二进制数据文件.MAT文件可以包含一个或者多个MATLAB 变量.MATLAB通常采用MAT文件把工作空间的变量存储在磁盘里,在MAT文件中不仅保存 ...