.net 关于路径的总结

原文:https://www.cnblogs.com/hehehehehe/p/6196155.html

  https://www.cnblogs.com/yugongmengjiutian/articles/5521165.html

绝对路径

绝对路径就是你的主页上的文件或目录在硬盘上真正的路径。比如:E:新概念英语新版新概念英语第二册课文PDF.pdf。以Web 站点根目录为参考基础的目录路径。之所以称为绝对,意指当所有网页引用同一个文件时,所使用的路径都是一样的。

1 "/" -- 代表根目录,绝对路径。 
2 如:<a href="/abc">文本</a> 或 <img src="/abc" />
3 "D:/abc/" -- 代表根目录,绝对路径。

相对路径

以引用文件之网页所在位置为参考基础,而建立出的目录路径。因此,当保存于不同目录的网页引用同一个文件时,所使用的路径将不相同,故称之为相对。

以下为建立路径所使用的几个特殊符号,及其所代表的意义。

复制代码
 1 "." -- 代表目前所在的目录,相对路径。 
 2 
 3 如:<a href="./abc">文本</a> 或 <img src="./abc" />
 4 
 5 ".." -- 代表上一层目录,相对路径。 
 6 
 7 如:<a href="../abc">文本</a> 或 <img src="../abc" />
 8 "../../" -- 代表的是上一层目录的上一层目录,相对路径。
 9 
10  如:<img src="../../abc" />
复制代码

在使用相对路径时,我们用符号“.”来表示当前目录,用符号“..”来表示当前目录的父目录。

/ 是超文本协议的路径分隔符号,所有的网站在浏览器中显示的路径分隔都是以"/"表示.它一般代表虚拟路径.

在普通程序代码中则以""表示文件路径分隔符号.它一般指物理路径.

 ./ 表示在当前路径下,

 ../表示在当前路径的上一级路径下.

~/表示当前网站的根目录下.

还有 /和\ 是等值得的!

在表示路径时有两大体系:
1、在windows下表示文件的位置,用反斜杠“” ,例如:E:新概念英语新版新概念英语第二册课文PDF.pdf

2、网络路径,例如http://www.i.cnblogs.com/EditPosts.aspx.

注意:路径中/和反斜杠的区别,在操作ftp中用到的也是网络路径,如果使用,是解析不了的,之前在项目中,就吃过这方面的亏。

总结

 ~/在runat=server的控件中,自动的被解析为Request.ApplicationPath的值,是当前应用程序级程序的目录(用在服务器控件中)。

/:表示当前目录。

../表示上一层目录。

/表示根目录。

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1.    在.Net中web开发时

(1)  ~/在runat=server的控件中会自动被解析为Request.ApplicationPath的值,是当前应用程序的目录 如

~/userCommunity/index.aspx则对应为/HENU.RCenter.Internal/UserCommunity

(2) ./表示当前目录

(3)../表示上一层目录 如UserCommunity文件夹下的文件中可以以:../module/来访问module中的文件

2 获取当前请求页面的路径:Request.FilePath

3 获取项目下的文件路径:

string path=AppDomain. CurrentDomain .SetUpInformation.ApplicationBase+文件夹+文件

如获取项目下的temp文件夹下文件的路径

可以用:string savePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "temp" + @"" + filename

4 Server.MapPath用法:

若在项目下Content文件夹下的UserInfoManager.aspx代码中写如下路径

this.tempPath = Server.MapPath("UploadResourceImage\");

则返回 D:wxm练习ContentUploadResourceImage

其它

一、获取当前文件的路径  

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

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

3.  System.IO.Directory.GetCurrentDirectory()    获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:www里,这个函数有可能返回C:Documents and SettingsYB\,或者C:Program FilesAdobe\,有时不一定返回什么东东,这是任何应用程序最后一次操作过的目录,比如你用Word打开了E:docmy.doc这个文件,此时执行这个方法就返回了E:doc了。   

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

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

6. System.Windows.Forms.Application.StartupPath    获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个""而已   

7. System.Windows.Forms.Application.ExecutablePath    获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。   

二、操作环境变量    利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系统环境变量,如:    System.Environment.GetEnvironmentVariable("windir")就可以取得windows系统目录的路径。   

以下是一些常用的环境变量取值:   

System.Environment.GetEnvironmentVariable("windir");

System.Environment.GetEnvironmentVariable("INCLUDE");   

System.Environment.GetEnvironmentVariable("TMP");   

System.Environment.GetEnvironmentVariable("TEMP");   

System.Environment.GetEnvironmentVariable("Path");   

最后贴出我进行上面操作获得的变量值,事先说明,本人是编写了一个WinForm程序,项目文件存放于D:Visual Studio ProjectsMyApplicationLifeAssistant,编译后的文件位于D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug,最后的结果如下:

1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebugLifeAssistant.exe   

2、System.Environment.CurrentDirectory=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug   

3、System.IO.Directory.GetCurrentDirectory()=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug   

1 asp.net webform用"Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含"";   

2.c# winform用   

A:"Application.StartupPath":获取当前应用程序所在目录的路径,最后不包含"";   

B:"Application.ExecutablePath ":获取当前应用程序文件的路径,包含文件的名称;   

C:"AppDomain.CurrentDomain.BaseDirectory":获取当前应用程序所在目录的路径,最后包含"";   

D:"System.Threading.Thread.GetDomain().BaseDirectory":获取当前应用程序所在目录的路径,最后包含"";   

E:"Environment.CurrentDirectory":获取当前应用程序的路径,最后不包含"";   

F:"System.IO.Directory.GetCurrentDirectory":获取当前应用程序的路径,最后不包含"";    3.c# windows service用"AppDomain.CurrentDomain.BaseDirectory"或"System.Threading.Thread.GetDomain().BaseDirectory";    用"Environment.CurrentDirectory"和"System.IO.Directory.GetCurrentDirectory"将得到" system32"目录的路径;   

如果要使用"Application.StartupPath"或"Application.ExecutablePath ",需要手动添加对"System.Windows.Forms.dll "的引用,并在程序开头用"using System.Windows.Forms"声明该引用;   

4.在卸载程序获取系统安装的目录:   

System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();   

string path=curPath.Location;//得到安装程序类SetupLibrary文件的路径,获取这个文件路径所在的目录即得到安装程序的目录;   

4、System.AppDomain.CurrentDomain.BaseDirectory=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug   

5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug   

6、System.Windows.Forms.Application.StartupPath=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebug   

7、System.Windows.Forms.Application.ExecutablePath=D:Visual Studio ProjectsMyApplicationLifeAssistantinDebugLifeAssistant.exe    System.Environment.GetEnvironmentVariable("windir")=C:WINDOWS   

System.Environment.GetEnvironmentVariable("INCLUDE")=C:Program FilesMicrosoft Visual Studio .NET 2003SDKv1.1include   

System.Environment.GetEnvironmentVariable("TMP")=C:DOCUME~1zhoufoxcnLOCALS~1Temp   

System.Environment.GetEnvironmentVariable("TEMP")=C:DOCUME~1zhoufoxcnLOCALS~1Temp   

System.Environment.GetEnvironmentVariable("Path")=C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:jdk1.5.0in;C:MySQLServer5.0in;C:Program FilesSymantecpcAnywhere;C:Program FilesMicrosoft SQL Server80ToolsBINN   

C# 相对路径 系统路径   

//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。 

string  str5=Application.StartupPath;    //可获得当前执行的exe的文件名。 

string  str1  =Process.GetCurrentProcess().MainModule.FileName;    //获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。备注  按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如"C:")。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如"C:mySubDirectory")。

  string  str2=Environment.CurrentDirectory;    //获取应用程序的当前工作目录。

  string  str3=Directory.GetCurrentDirectory();    //获取基目录,它由程序集冲突解决程序用来探测程序集。  string  str4=AppDomain.CurrentDomain.BaseDirectory;    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。  string  str5=Application.StartupPath;    //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。 

string  str6=Application.ExecutablePath;    //获取或设置包含该应用程序的目录的名称。    string  str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;    //例子    Application.StartupPath;    //可以得到F:learningc#TrainingwinwininDebug    //注意自己补两个    Application.StartupPath+"\3.jpg";

在c#中,相对路径是用"."和".."表示,   "."代表当前目录,    ".."代表上一级录。    例如 假设我用vs2005在D:My DocumentsVisual Studio 2005Projects目录里创建了一个名叫controls的项目,即在Projects文件夹里有一个controls文件夹,controls文件夹里有三个文件:controls.sln  controls文件夹  GulfOfStLawrence文件夹。    D:My DocumentsVisual Studio 2005ProjectsControlsControlsinDebug是这个简单项目能够运行的可执行文件Controls.exe    现在我想要 D:My DocumentsVisual Studio 2005ProjectsControlsGulfOfStLawrence文件夹下的Gulf_of_St._Lawrence.mxd(arcgis desktop)工程文件路径。    那么相对路径应该就是"......GulfOfStLawrenceGulf_of_St._Lawrence.mxd"    即string filename = @"......GulfOfStLawrenceGulf_of_St._Lawrence.mxd";   

心得:

1.用相对路径能增加项目的可移植性。使一个工程在移植过程中变得简单,节省了大量布置与工程相关的文件的时间。(如果设置的是绝对路径)。 

2.使用相对路径也使程序代码变得简单 

3. 但有一点必须注意:(只能在同一个驱动器里(如:都在D:里)使用相对路径)。

原文地址:https://www.cnblogs.com/zhang1f/p/11831057.html