当前位置: 移动技术网 > IT编程>开发语言>.net > C# ,asp.net 获取当前,相对,绝对路径

C# ,asp.net 获取当前,相对,绝对路径

2019年05月09日  | 移动技术网IT编程  | 我要评论

hotmail邮箱,我是你额上熏黑的矿灯,阿拉拜犬

一、c#获取当前路径的方法:

1. system.diagnostics.process.getcurrentprocess().mainmodule.filename

-获取模块的完整路径。

2. system.environment.currentdirectory

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

3. system.io.directory.getcurrentdirectory()

-获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在c:\www里,这个函数有可能返回c:\documents and settings\zyb\,或者c:\program files\adobe\。

4. system.appdomain.currentdomain.basedirectory

-获取程序的基目录。

5. system.appdomain.currentdomain.setupinformation.applicationbase

-获取和设置包括该应用程序的目录的名称。

6. system.windows.forms.application.startuppath

-获取启动了应用程序的可执行文件的路径。

7. system.windows.forms.application.executablepath

-获取启动了应用程序的可执行文件的路径及文件名。

二、操作环境变量
利用system.environment.getenvironmentvariable()方法可以很方便地取得系统环境变量,如:
system.environment.getenvironmentvariable("windir")就可以取得windows系统目录的路径。
以下是一些常用的环境变量取值:
system.environment.getenvironmentvariable("windir")=c:\windows
system.environment.getenvironmentvariable("include")=c:\program files\microsoft visual studio .net 2003\sdk\v1.1\include\
system.environment.getenvironmentvariable("tmp")=c:\docume~1\zhoufoxcn\locals~1\temp
system.environment.getenvironmentvariable("temp")=c:\docume~1\zhoufoxcn\locals~1\temp
system.environment.getenvironmentvariable("path")=c:\windows\system32;c:\windows;c:\windows\system32\wbem;c:\jdk1.5.0\bin;c:\mysqlserver5.0\bin;c:\program files\symantec\pcanywhere\;c:\program files\microsoft sql server\80\tools\binn

三、关于"\"

1 asp.net webform

a:request.physicalapplicationpath获取站点所在虚拟目录的物理路径,最后包含“\”;
2.c# winform
a:“application.startuppath”:获取当前应用程序所在目录的路径,最后不包含“\”;
b:“application.executablepath ”:获取当前应用程序文件的路径,包含文件的名称;如:export.exe
c:“appdomain.currentdomain.basedirectory”:获取当前应用程序所在目录的路径,最后包含“\”;
d:“system.threading.thread.getdomain().basedirectory”:获取当前应用程序所在目录的路径,最后包含“\”;
e:“environment.currentdirectory”:获取当前应用程序的路径,最后不包含“\”;
f:“system.io.directory.getcurrentdirectory”:获取当前应用程序的路径,最后不包含“\”;

四、程序卸载获取系统安装目录

system.reflection.assembly curpath = system.reflection.assembly.getexecutingassembly();
string path=curpath.location;//得到安装程序类setuplibrary文件的路径,获取这个文件路径所在的目录即得到安装程序的目录;

五、asp.net 获取路径

1.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

2.request.filepath->对应于iis的虚拟目录 
如 url http://mockte.com/1//pathinfo 
filepath = /1/

3.request.path->当前请求的虚拟路径 
path 是 filepath 和 pathinfo 尾部的串联。例如 url http://mockte.com/1//pathinfo 
那么path = /1//pathinfo

4.request.mappath(string url)->将url映射为iis上的虚拟目录 
这个目录都是相对于application的根目录的 
于server.mappath相比,不会包含类似c:/这样的路径 
可以理解为是相对路径(对比的server.mappath就是绝对路径)

5.server.mappath(string url)->将url映射为服务器上的物理路径 
例如 http://mockte.com/1/ 假设你的应用程序在c:/iis/mysite中 
那么就是 c:/iis/mysite/1/

路径转换代码:
//本地路径转换成url相对路径
private string urlconvertor(string imagesurl1)
{
  string tmprootdir = server.mappath(system.web.httpcontext.current.request.applicationpath.tostring());//获取程序根目录
  string imagesurl2 = imagesurl1.replace(tmprootdir, ""); //转换成相对路径
  imagesurl2 = imagesurl2.replace(@"/", @"/");
  //imagesurl2 = imagesurl2.replace(@"aspx_uc/", @"");
  return imagesurl2;
}
//相对路径转换成服务器本地物理路径
private string urlconvertorlocal(string imagesurl1)
{
  string tmprootdir = server.mappath(system.web.httpcontext.current.request.applicationpath.tostring());//获取程序根目录
  string imagesurl2 = tmprootdir + imagesurl1.replace(@"/", @"/"); //转换成绝对路径
  return imagesurl2;
}

 

1.使用filepath="/logs/abc.txt",被认为是根目录,即网页文件所在的盘符,默认的是c盘,则在这里这个路径被解释为"c:/logs/abc.txt"

2.使用filepath="~/logs/abc.txt",被认为是服务器的目录

3.使用filepath="./logs/abc.txt",仍然是服务器目录下

 

转自:c# ,asp.net 获取当前,相对,绝对路径

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网