
使用 ApplicationStartupPath
至于
SystemIODirectoryGetCurrentDirectory不行,因为应用程序有个当前工作目录,这个工作目录是会变的,不总是程序的启动目录(当然默认启动是应用程序目录)。
比如说,你打开命令行(cmd),它就会显示一个路径,你会发现这个路径(通常是 C:\Documents and settings\用户名,在XP下)不是cmdexe所在目录(cmdexe 是在system32下的)。
而这个当前目录很容易改变,甚至当你使用了文件浏览对话框(比如打开文件、保存文件)时,这个当前目录都会改变到它最后选择的目录。
而快捷方式中的启动目录,就是设置当前应用程序的工作目录。默认情况下,新建的快捷方式的启动目录就是应用程序所在目录,所以你不修改的话是可以正常工作的,明白了?
1 SystemDiagnosticsProcessGetCurrentProcess()MainModuleFileName
-获取模块的完整路径。
2SystemEnvironmentCurrentDirectory
-获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
3SystemIODirectoryGetCurrentDirectory()
-获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,
4SystemAppDomainCurrentDomainBaseDirectory
-获取程序的基目录。
5SystemAppDomainCurrentDomainSetupInformationApplicationBase
-获取和设置包括该应用程序的目录的名称。
6 SystemWindowsFormsApplicationStartupPath
-获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已
7SystemWindowsFormsApplicationExecutablePath
-获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
对于Windows程序 和Web 应用程序来说,他们运行的路径是不一样的,所以关键是判断当前运行的程序是哪种程序于是我们可以使用如下的代码
string path = "";
if (SystemEnvironmentCurrentDirectory ==AppDomainCurrentDomainBaseDirectory)//Windows应用程序则相等
{
path = AppDomainCurrentDomainBaseDirectory;
}
else
{
path = AppDomainCurrentDomainBaseDirectory + "Bin\";
}
这样如果我们写了一个类库,类库中用到了AssemblyLoadFrom,由于是通用类库,所以可能用到Windows程序中也可能用到Web中,那么用上面的代码就很方便了
1、ServerMapPath
2、SystemWindowsFormsStartupPath
3、TypeAssemblyLocation
方法2可以应用于控制台应用程序,WinForm应用程序,Windows服务,方法1可以应用于Web应用程序,方法3都可以应用。
但方法3是加载应用程序的路径。如果是Web应用程序,取得的路径是:C:\WINDOWS\MicrosoftNET\Framework\v114322\Temporary ASPNET Files目录。所以Web项目还是使用ServerMapPath吧。否则建议使用方法2。如果自己新建类库。可以加入对SystemWindowsFormsStartupPath的引用后使用
DirectoryInfo dir = new DirectoryInfo(ApplicationStartupPath)ParentParent;
string target = dirFullName;
在java中获得文件的路径在我们做上传文件 *** 作时是不可避免的。
web 上运行
1:thisgetClass()getClassLoader()getResource("/")getPath();
thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/
SystemgetProperty("userdir");
thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 项目的绝对路径。
如:/D:/jboss-422GA/server/default/deploy/hpwar
2:thisgetClass()getResource("/")getPath();
thisgetClass()getResource("")getPath(); 得到的是当前类 文件的URI目录。
如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/com/jebel/helper/
thisgetClass()getResource("")getPath(); X 不 能运行
3:ThreadcurrentThread()getContextClassLoader()getResource("/")getPath()
ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-422GA/server/default/deploy/hpwar/WEB-INF/classes/
ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 项目的绝对路径。
如:/D:/jboss-422GA/server/default/deploy/hpwar
在本地运行中
1:thisgetClass()getClassLoader()getResource("")getPath();
thisgetClass()getClassLoader()getResource("")getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
thisgetClass()getClassLoader()getResource("")getPath(); X 不 能运行
2:thisgetClass()getResource("")getPath();
thisgetClass()getResource("")getPath(); 得到的是当前类 文件的URI目录。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
3:ThreadcurrentThread()getContextClassLoader()getResource("")getPath()
ThreadcurrentThread()getContextClassLoader()getResource("")getPath() 得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
ThreadcurrentThread()getContextClassLoader()getResource("/")getPath() X 不 能运行
最后
在Web应用程序中,我们一般通过ServletContextgetRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有requestgetContextPath(); 在Weblogic中要用requestgetServletContext()getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。
NET的吧
可以参考这里,有详细讲解和示例代码:
>
我对楼主问题的理解是想找出应用工程的位置,那么我们可以用这个办法:
右键单击一下,选择“打开文件位置”
这个应用程序名就是12,就这样我们找到了自己的文件
以上就是关于如何获取应用程序的绝对路径全部的内容,包括:如何获取应用程序的绝对路径、C#如何获得文件夹的路径、c# 窗体应用怎么获取当前项目的路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)