时间: 2021-07-31 作者:daque
获适合长进序文件的道路也算是一个比较常见的须要,比如我有一个文件conn.xml坐落.exe的同一目录,在运行时需要居中读取一些竖立动静。
string str1 =process.getcurrentprocess().mainmodule.filename;//可博得姑且举行的exe的文件名。
string str2=environment.currentdirectory;//赢得和竖立姑且目录(即该进程居中起用的目录)的十足遏制道路。
//备注 按照树立,纵然该进程在本地或收集启用器的根目录中起用,则此属性的值为启用器称谓后跟一个尾部反斜杠(如“c:\”)。纵然该进程在子目录中启 动,则此属性的值为不带尾部反斜杠的启用器和子目录道路(如“c:\mysubdirectory”)。
string str3=directory.getcurrentdirectory();//赢得应用步伐的姑且处世目录。
string str4=appdomain.currentdomain.basedirectory;//赢得基目录,它由步伐集商量处治步伐用来探测步伐集。
string str5=application.startuppath;//赢得起用了应用步伐的可举行文件的道路,不包括可举行文件的称谓。
string str6=application.executablepath;//赢得起用了应用步伐的可举行文件的道路,包括可举行文件的称谓。
string str7=appdomain.currentdomain.setupinformation.applicationbase;//赢得或竖立囊括该应用步伐的目录的称谓。
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
赢得起用了应用步伐的可举行文件的道路。工效和2、5一致。然而5返来的字符串背后多了一个"\"结束
7. system.windows.forms.application.executablepath
赢得起用了应用步伐的可举行文件的道路及文件名,工效和1一致。
对于windows步伐 和web 应用步伐来说,她们运行的道路是不一致的,以是重要是决定姑且运行的步伐是哪种步伐.以是我们无妨应用如次的代码 string path = "";
if (system.environment.currentdirectory == appdomain.currentdomain.basedirectory)//windows应用步伐则格外
{
path = appdomain.currentdomain.basedirectory;
}
else
{
path = appdomain.currentdomain.basedirectory + "bin\";
}
多么纵然我们写了一个类库,类库中用到了assembly.loadfrom,由于是通用类库,以是大约用到windows步伐中也大约用到web中,那么用上面的代码就很大略了.
1、server.mappath
2、system.windows.forms.startuppath
3、type.assembly.location
本事2无妨应用于遏止台应用步伐,winform应用步伐,windows功效,本事1无妨应用于web应用步伐,本事3都无妨应用。
但本事3是加载应用步伐的道路。纵然是web应用步伐,赢得的道路是c:\windows\microsoft.net\framework\v1.1.4322\temporary asp.net files目录。以是web花样保持应用server.mappath吧。要不倡议应用本事2。纵然自己兴修类库。无妨加入对system.windows.forms.startuppath的引用后应用。
进程货色在.net中展示为system.diagnostics.process类,过程调用process.getcurrentprocess().mainmodule.filename可博得姑且举行的exe的文件名。但是这个本事赢得的不过是文件名,纵然步伐运行工夫没有切换处世目录,那么无妨调用system.io.path的本事赢得普遍道路。但是姑且目录同样无妨过程environment.currentdirectory博得,而且很多软硬件在应用open dialog翻开文件的工夫,都会切换处世目录,而使得这一机制废除。
纵然是在windows forms应用步伐中,姑且应用也展示为system.windows.forms.application货色,过程其静态属性application.executablepath和application.startuppath,无妨赢得可举行文件的道路和起用道路。
但纵然不是在windows应用中呢,大约是在library中呢,就算是application货色的属性保持能博得,也需要在工程中填补system.windows.forms这个assembly的引用,特出不大略。这个工夫,无妨过程assembly的静态本事,getcallingassembly大约getexecutingassembly赢得姑且举行的assembly,尔后过程assembly类的location赢得assembly的场合。
但是应用assembly的工夫,大约会遇到权利上头的标题,同声assembly.getcallingassembly大约assembly.getexecutingassembly有大约赢得的不是.exe文件的场合。在gac中的填补了强名的assembly,运行时是无妨不必与.exe在同一目录的。
.net的进程起用时,会创作appdomain,十足的assembly都被load到某一个appdomain中,而appdomain中需要了setupinformation属性,无妨赢得appdomain起用时的一些动静,以是,无妨过程调用appdomain.currentdomain.setupinformation.applicationbase赢得姑且应用步伐场合的道路。
在过程之上本聆取到所需的目录后,无妨调用system.io.path的本事赢得文件名,目录名,普遍道路等。遏制对道路字符串的领略,而改用system.io.path类吧。
开辟.net下应用步伐时,领略process/application->appdomain->assembly的接洽,对于举行透彻的论理,厉害常有辅助的。