c# 获得当前绝对路径的方法(超简单)

时间:2021-11-02 07:05:23

废话不多说,直接上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <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# 获得当前绝对路径的方法(超简单)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/highest/p/8301376.html