文件处理系列--相对路径转绝对路径

1)System.IO.Path.GetFullPath("")(适用于客户端取地址)

控制程序测试

 string url = "Common/Img/1579081307.jpg";
            Console.WriteLine("Common/Img/1579081307.jpg使用System.IO.Path.GetFullPath()转化:
"  + System.IO.Path.GetFullPath(url ));
//该URL绝对地址为:
//F:Project_testTestDemoProject2020TestDemoProject2020CommonImg1579081307.jpg
//输出后的地址为
//F:Project_testTestDemoProject2020TestDemoProject2020PageinDebugCommonImg1579081307.jpg

服务器端测试

//应用在web程序,服务器端的,取地址为IIS对应的exe地址
Response.Write(System.IO.Path.GetFullPath("Demo01/DemoRelative.aspx") +"<br/>");
//输出地址
//C:Program Files (x86)IIS ExpressDemo01DemoRelative.aspx

总结:使用该方式相对地址转绝对地址,定位为该客户端程序exe所在的位置


2)HttpContext.Current.Server.MapPath("")(适用于服务器端)

  • Server.MapPath(".")

    返回正在执行的文件的当前物理目录(例如aspx)。
  • Server.MapPath("..")

    返回父目录。
  • Server.MapPath("~")

    返回应用程序根的物理路径。
  • Server.MapPath("/")

    返回域名根的物理路径(不一定与应用程序的根相同)

例如:

假设您指向了一个网站应用程序(http://www.example.com/)到 C:Inetpubwwwroot

并将您的商店应用程序(子web作为IIS中的虚拟目录,标记为应用程序)安装在 D:WebAppsshop

例如,如果你打电话给Server.MapPath()在以下请求中:http://www.example.com/shop/products/GetProduct.aspx?id=2342

然后:

  • Server.MapPath(".")

    D:WebAppsshopproducts

  • Server.MapPath("..")

    D:WebAppsshop

  • Server.MapPath("~")

    D:WebAppsshop

  • Server.MapPath("/")

    C:Inetpubwwwroot

  • Server.MapPath("/shop")

    D:WebAppsshop

如果路径以正斜杠开头(/)或向后斜杠(),MapPath()返回路径,就像路径是一个完整的虚拟路径一样。

如果路径不以斜杠开头,则MapPath()返回相对于正在处理的请求的目录的路径。

注:在C#中,@是逐字面值字符串运算符,意思是字符串应按原样使用,而不是对转义序列进行处理。

若应用在控制程序,HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。原因是无http请求,下面进行处理

            if (HttpContext.Current != null)
            {
                Console.WriteLine("该地址使用HttpContext.Current.Server.MapPath:
" + HttpContext.Current.Server.MapPath(url));//有http请求
            }
            else //非web程序引用             
            {
                url = url.Replace("/", "\");
                if (url.StartsWith("\"))
                {
                    url = url.TrimStart('\');
                }
                Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
                Console.WriteLine("该地址使用HttpContext.Current.Server.MapPath:
" + System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, url));
            }
//补充内容 

// 获取程序的基目录。
System.AppDomain.CurrentDomain.BaseDirectory

// 获取模块的完整路径,包含文件名
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
System.Environment.CurrentDirectory

// 获取应用程序的当前工作目录,注意工作目录是可以改变的,而不限定在程序所在目录。
System.IO.Directory.GetCurrentDirectory()

// 获取和设置包括该应用程序的目录的名称。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

// 获取启动了应用程序的可执行文件的路径。
System.Windows.Forms.Application.StartupPath

// 获取启动了应用程序的可执行文件的路径及文件名
System.Windows.Forms.Application.ExecutablePath

3)使用URI

/// <summary>
/// 格式化URL函数  urlX 传入相对URL
/// objurl 传入绝对基URL  基URL 一定要带HTTP://
/// </summary>
/// <param name="urlX">传入单个的URL</param>
/// <param name="objurl">
/// 传入得到值的页面URL
/// </param>
/// <returns></returns>
public String FormAturl(String urlX, string objurl)
{
    Uri baseUri = new Uri(objurl); // objurl 为http://www.enet.com.cn/enews/inforcenter/designmore.jsp
    Uri absoluteUri = new Uri(baseUri, urlX);//相对绝对路径都在这里转 这里的urlx ="../test.html"
    return absoluteUri.ToString();//   http://www.enet.com.cn/enews/test.html   
}

补充URI

URI,通一资源标志符(Uniform Resource Identifier, URI),表示的是web上每一种可用的资源,如 HTML文档、图像、视频片段、程序等都由一个URI进行定位的。

URI通常由三部分组成:①访问资源的命名机制;②存放资源的主机名;③资源自身的名称。

URI举例

如:https://blog.csdn.net/qq_32595453/article/details/79516787

我们可以这样解释它:

①这是一个可以通过https协议访问的资源,

②位于主机 blog.csdn.net上,

③通过“/qq_32595453/article/details/79516787”可以对该资源进行唯一标识(注意,这个不一定是完整的路径)

注意:以上三点只不过是对实例的解释,以上三点并不是URI的必要条件,URI只是一种概念,怎样实现无所谓,只要它唯一标识一个资源就可以了。

原文地址:https://www.cnblogs.com/TechSingularity/p/11969555.html