C#获取项目程序路径的方法

来源:这里教程网 时间:2026-02-21 13:05:26 作者:

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文件的路径,获取这个文件路径所在的目录即得到安装程序的目录 

http://blog.163.com/dykj_dxj/blog/static/254925252010830113424205/

相关推荐