.CS
HttpContext.Current.Server.MapPath();//所在文件夹路径
System.Web.HttpContext.Current.Request.PhysicalApplicationPat;//根路径
.aspx
Server.MapPath(Request.ServerVariables["PATH_INFO"]) //页面详细路
Server.MapPath("/") //根目录
Server.MapPath("") //当前代码文件所在的目录路径
Server.MapPath(".")
Server.MapPath("../") //当前代码所在路径的上级路径
Server.MapPath("..")
Page.Request.ApplicationPath
WinForm
获取 当前执行程序路径的几种方法
string str = System.Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径
string str = System.Windows.Forms.Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名。
string str = System.AppDomain.CurrentDomain.BaseDirectory;//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.IO.Directory.GetCurrentDirectory();//获取应用程序的当前工作目录
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取和设置包含该应用程序的目录的名称。
string str = this.GetType().Assembly.Location;//获取当前进程的完整路径,包含文件名
string str = System.Windows.Forms.Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
Asp.net
获取当前目录的三种方法
方法一:
string sPath = System.IO.Path.GetDirectoryName(Page.Request.PhysicalPath)
方法二:
string sPath = System.Web.HttpContext.Current.Request.MapPath(“/“)
方法三:string sPath = Page.Server.MapPath(“/“);
我推荐使用第二种
有时候可能出现找不到类的情况,请引用System.Web.dll
Server.MapPath() 和 Request.MapPath()使用区别
一、Server.MapPath()
应用
假设当前的网站目录为E:\wwwroot
应用程序虚拟目录为E:\wwwroot\company
浏览的页面路径为E:\wwwroot\company\news\
下面的一个 aspx
页面。
在该页面中使用
Server.MapPath(“”) :返回当前页面所在的物理文件路径:E:\wwwroot\company\news
Server.MapPath(“/”) :返回应用程序根目录所在的物理文件路径:E:\wwwroot
Server.MapPath(“./”) :返回当前页面所在的物理文件路径:E:\wwwroot\company\news
Server.MapPath(“../”):返回当前页面所在的上一级的物理文件路径:E:\wwwroot\company
Server.MapPath(“~/”):返回应用程序的虚拟目录(路径):E:\wwwroot\company
Server.MapPath(“~”):返回应用程序的虚拟目录(路径):E:\wwwroot\company
二、ASP.NET
中Server.MapPath()
和 Request.MapPath()
使用区别:
Server.MapPath(string) :是将相对于当前调用文件的文件(或目录)映射为物理路径;
Request.MapPath(string) :是将string虚拟路径映射为物理路径(asp中Request无此方法)
Server.MapPath(string) 中string 可以用“../”方式引用父目录,甚至可以将此目录跳到整个WEB目录外,如:C:\WWWROOT
目录为WEB根目录,在根目录文件中调用此Server.MapPath(“../脚本文件”),则可以调用WEB目录外的脚本、资源等。
Request.MapPath(string) 中的string为虚拟目录,只能相对WEB虚拟目录形式的,也不允许”../”方式调用,只能是”/”,”/xx”等字符串
string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。
string str2=Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
Server.MapPath
和Request.MapPath()
的用法
./
当前目录
/
根目录
../
上层目录(相对当前来说)
如果当前的网站目录为D:\wwwroot
浏览的页面路径为D:\wwwroot\company\news\show.asp
在show.asp
页面中使用
Server.MapPath("./") 返回路径为:D:\wwwroot\company\news
Server.MapPath("/") 返回路径为:D:\wwwroot
Server.MapPath("../") 返回路径为:D:\wwwroot\company
server.MapPath(request.ServerVariables("Path_Info"))
Request.ServerVariables("Path_Translated")
上面两种方式返回路径为 D:\wwwroot\company\news\show.asp
ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath
Request.ApplicationPath
->当前应用的目录
Jsp中,ApplicationPath
指的是当前的application
(应用程序)的目录,ASP.NET中也是这个意思。
对应的–例如我的服务器上有两个web应用域名都是mockte.com
一个映射到目录mockte.com/1/
另一个影射到http://mockte.com/2/
那么mockte.com/1/
就是第一个应用的ApplicationPath
同理mockte.com/2/
就是第二个应用的ApplicationPath
HttpRequest.ApplicationPath
表示应用程序虚拟目录的根路径
需要动态设置虚拟目录的时候,需要用此属性值,
一般可以类似这样使用 - 》
<img src='<% =Request.ApplicationPath == "/" ? "" : Request.ApplicationPath %>'/images/some.gif' /> // 假设 images 文件再你的根目录
-
Request.FilePath
->对应于iis的虚拟目录
如URL http://mockte.com/1/index.html/pathinfo
FilePath = /1/index.html
-
Request.Path
->当前请求的虚拟路径
Path
是FilePath
和PathInfo
尾部的串联。例如 URL http://mockte.com/1/index.html/pathinfo
那么Path = /1/index.html/pathinfo
URL Http://www.contoso.com/virdir/page.html/tail,PathInfo 值为 /tail。
-
Request.MapPath(string url)
->将url映射为iis上的虚拟目录
这个目录都是相对于application
的根目录的
于Server.MapPath
相比,不会包含类似c:/
这样的路径
可以理解为是相对路径(对比的Server.MapPath
就是绝对路径) -
Server.MapPath(string url)
->将url映射为服务器上的物理路径
例如http://mockte.com/1/index.html
假设你的应用程序在c:/iis/MySite
中
那么就是c:/iis/MySite/1/index.html