c#如何获得当前绝对路径

这篇文章给大家分享的是有关c#如何获得当前绝对路径的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

代码

/// <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)
      {
        string path = HttpContext.Current.Server.MapPath("~/" + strPath);
        return path;
      }
      else //非web程序引用
      {
        strPath = strPath.Replace("/", "\\");
        if (strPath.StartsWith("\\"))
        {
          strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
        }
        return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
      }
    }

感谢各位的阅读!关于“c#如何获得当前绝对路径”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!