因工作的时候需要用的,这个是比较齐全的,记录下以备不时之需
(1)Application.ExecutablePath
获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
(2)Application.StartupPath
获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
即Application.StartupPath是路径Application.ExecutablePath去掉文件名:
Application.StartupPath等价与
System.IO.Directory.GetParent(Application.ExecutablePath).FullName
(3)Environment.CurrentDirectory
获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
这个值显然不是exe文件所在的目录。而是最后一次的用户访问目录,例如,如果在程序中您使用Dialogbox选定了某一个目录的文件那么这个目录就会随之改变。
测试程序如下:
string sStr = string.Format("Application.ExecutablePath:{0}\nApplication.StartupPath:{1}\nEnvironment.CurrentDirectory:{2}", Application.ExecutablePath, Application.StartupPath, Environment.CurrentDirectory);
Console.WriteLine(sStr);
输出结果为:
Application.ExecutablePath:E:\JinpengGIS\Debug\Bin\JinpengGIS.Main.EXE
Application.StartupPath:E:\JinpengGIS\Debug\Bin
Environment.CurrentDirectory:E:\JinpengGIS\Debug\Data
二、ASP.Net路径
(1)Request.ApplicationPath
获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。
(2)Request.CurrentExecutionFilePath
获取当前请求的虚拟路径。
(3)Request.FilePath
获取当前请求的虚拟路径。
FilePath 属性不包含 PathInfo 尾端;例如,对于 URL:http://www.contoso.com/virdir/page.html/tail,FilePath 值为 http://www.contoso.com/virdir/page.html。
(4)Request.Path
获取当前请求的虚拟路径。同Request.FilePath一样:
Path 不包含 PathInfo 尾部;例如,对于 URL:http://www.contoso.com/virdir/page.html/tail,Path 为 http://www.contoso.com/virdir/page.html/tail。
注意:
CurrentExecutionFilePath 会返回重定向方案(例如 Execute 和 Transfer)中当前正在执行的页的正确文件路径。当客户端重定向到另一页时,FilePath 返回初始页的路径;CurrentExecutionFilePath 返回子页的路径。当使用 Transfer 或 Execute 方法时,CurrentExecutionFilePath 指向当前正在执行的处理程序。
(5)Request.PhysicalApplicationPath
获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。
(6)Request.PhysicalPath
获取与请求的 URL 相对应的物理文件系统路径。
(7)Request.RawUrl
获取当前请求的原始 URL。
原始 URL 定义为 URL 中域信息之后的部分。在 URL 字符串 http://www.contoso.com/articles/recent.aspx 中,原始 URL 为 /articles/recent.aspx。原始 URL 包括查询字符串(如果存在)。
(8)HostingEnvironment.MapPath
将虚拟路径映射到服务器上的物理路径。
(9)Server.MapPath
返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
ASP.Net常用路径举例,如下:
// 根目录虚拟路径,即解决方案名称
string virtualPath = Page.Request.ApplicationPath;
// 根目录在服务器上的绝对路径
string pathRooted = HttpContext.Current.Server.MapPath(virtualPath);
//根目录在服务器上的Output文件夹下的Legend.bmp文件路径,对应与解决方案Output文件夹下的Legend.bmp文件路径