C# 取得网站根目录(Web使用)

分类:C#     发布:2019-08-09     来源:本站     浏览:2212 次
//--------------------------------------------------------------------------------
// 文件描述:路径辅助类
// 文件作者:全体开发人员
// 创建日期:2016-06-27
//--------------------------------------------------------------------------------

using System;
using System.IO;
using System.Web;

namespace RC.Framework
{
    public class PathHelper
    {
        /// <summary>
        /// 网站根目录
        /// </summary>
        public static string BasePath
        {
            get
            {
                if (HttpContext.Current != null)
                    return VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Request.ApplicationPath);
                return HttpRuntime.AppDomainAppVirtualPath.EndsWith("/")
                    ? HttpRuntime.AppDomainAppVirtualPath
                    : HttpRuntime.AppDomainAppVirtualPath + "/";
            }
        }

        /// <summary>
        /// 获得当前绝对路径
        /// </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);
            strPath = strPath.Replace("/", "\\");
            if (strPath.StartsWith("\\"))
                strPath = strPath.TrimStart('\\');
            else if (strPath.StartsWith("~")) strPath = strPath.Substring(1).TrimStart('\\');
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }
}
        

如果觉得文章对您有帮助,您可以对我进行打赏 ¥0.6 元(金额随机^_^,每次刷新金额不同)。

请使用支付宝扫码支付

留言评论

*称  呼:
*联系方式: 方便与您取得联系,推荐使用邮箱。
*内  容:

已有评论

暂无数据

上一篇:C# Object扩展方法(语法糖)

下一篇:C# 数据库访问类(MS SQL Server)