获取应用程序的path

我最近search了如何在Java中获得应用程序的目录。 我终于find了答案,但我需要惊人的长,因为寻找这样一个通用的术语并不容易。 我认为编制一份如何以多种语言实现这一目标的清单是一个好主意。

如果您(不)喜欢这个想法, 随意上/下行投票,如果您喜欢, 请投稿

澄清:

包含可执行文件目录当前工作目录 (由Unix下的pwd给出)之间有很好的区别。 我最初对前者感兴趣,但是也可以自由发布确定后者的方法(澄清你的意思)。

Java中的调用

 System.getProperty("user.dir") 

 new java.io.File(".").getAbsolutePath(); 

返回当前的工作目录。

打电话给

 getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 

返回包含当前类的JAR文件的path,或者如果直接从文件系统运行,则返回当前类的CLASSPATH元素(path)。

例:

  1. 你的应用程序位于

      C:\MyJar.jar 
  2. 打开shell(cmd.exe)并cd到C:\ test \子目录。

  3. 使用命令java -jar C:\MyJar.jar启动应用程序。

  4. 前两个调用返回'C:\ test \ subdirectory'; 第三个调用返回“C:\ MyJar.jar”。

从文件系统而不是JAR文件运行时,结果将是生成的类文件的根path

 c:\eclipse\workspaces\YourProject\bin\ 

该path不包括生成的类文件的包目录。

获取没有.jar文件名的应用程序目录的完整示例,或者直接从文件系统运行(例如debugging时)的类文件的相应path:

 String applicationDir = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); if (applicationDir.endsWith(".jar")) { applicationDir = new File(applicationDir).getParent(); } // else we already have the correct answer 

.NET(C#,VB,…)中 ,您可以查询当前的Assembly实例的Location 。 但是,这会附加可执行文件的名称。 以下代码清理path( using System.IOusing System.Reflection ):

 Directory.GetParent(Assembly.GetExecutingAssembly().Location) 

或者,您可以使用AppDomain提供的信息来search引用的程序集:

 System.AppDomain.CurrentDomain.BaseDirectory 

VB通过My命名空间允许另一个快捷方式:

 My.Application.Info.DirectoryPath 

Windows中 ,使用WinAPI函数GetModuleFileName() 。 传递NULL给模块句柄以获取当前模块的path。

python

 path = os.path.dirname(__file__) 

获取当前模块的path。

Objective-Ccocoa (Mac OS X,我不知道iPhone的具体情况):

 NSString * applicationPath = [[NSBundle mainBundle] bundlePath]; 

Java中 ,有两种方法来查找应用程序的path。 一个是使用System.getProperty

 System.getProperty("user.dir"); 

另一种可能性是使用java.io.File

 new java.io.File("").getAbsolutePath(); 

还有一种可能性使用反思:

 getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 

在VB6中,您可以使用App.Path属性获取应用程序path。

请注意,当应用程序位于驱动器的根目录下时,这不会有\ EXCEPT后缀。

在IDE中:

 ?App.Path C:\Program Files\Microsoft Visual Studio\VB98 

.Net中,您可以使用

System.IO.Directory.GetCurrentDirectory

获取应用程序的当前工作目录,并在VB.NET中专门可以使用

My.Application.Info.DirectoryPath

获取exe的目录。

delphi

在Windows应用程序中:

 Unit Forms; path := ExtractFilePath(Application.ExeName); 

在控制台应用程序

独立于语言,第一个命令行参数是完全限定的可执行文件名称:

 Unit System; path := ExtractFilePath(ParamStr(0)); 

libc库
在* nixtypes的环境(也在Windows中的Cygwin):

  #include <unistd.h> char *getcwd(char *buf, size_t size); char *getwd(char *buf); //deprecated char *get_current_dir_name(void); 

见手册页

Unix的

在unix中,可以find使用环境variables启动的可执行文件的path。 这不一定是绝对path,因此您需要将当前工作目录(在shell: pwd )和/或PATHvariables与环境的第0个元素的值相结合。

但是,这个值在unix中是有限制的,因为可执行文件可以通过符号链接进行调用,并且只有初始链接用于环境variables。 一般来说,如果unix的应用程序使用它来做任何有趣的事情(比如加载资源),它们并不是很健壮。 在unix上,通常使用硬编码的位置来指定事物,例如/etc中指定资源位置的configuration文件。

bash中 ,“pwd”命令返回当前的工作目录。

PHP中

 <?php echo __DIR__; //same as dirname(__FILE__). will return the directory of the running script echo $_SERVER["DOCUMENT_ROOT"]; // will return the document root directory under which the current script is executing, as defined in the server's configuration file. echo getcwd(); //will return the current working directory (it may differ from the current script location). ?> 

Android中

 getApplicationInfo().dataDir; 

拿到SD卡,我用

 Environment.getExternalStorageDirectory(); Environment.getExternalStoragePublicDirectory(String type); 

后者用于存储特定types的文件(audio/电影等)。 您在Environment类中具有这些string的常量。

基本上,任何与应用程序使用ApplicationInfo类和任何事情与SD卡/外部目录中的数据使用环境类。

文档: ApplicationInfo , Environment

Tcl

当前脚本的path:

 set path [info script] 

Tcl shellpath:

 set path [info nameofexecutable] 

如果您需要任何这些目录,请执行以下操作:

 set dir [file dirname $path] 

获取当前(工作)目录:

 set dir [pwd] 

Ruby中 ,以下片段返回当前源文件的path:

 path = File.dirname(__FILE__) 

CFML中 ,有两个函数用于访问脚本的path:

 getBaseTemplatePath() getCurrentTemplatePath() 

调用getBaseTemplatePath将返回“base”脚本的path – 即Web服务器请求的path。
调用getCurrentTemplatePath将返回当前脚本的path – 即当前正在执行的path。

这两个path都是绝对的,包含脚本的完整目录+文件名。

要确定目录,对结果使用函数getDirectoryFromPath( ... )

因此,要确定应用程序的目录位置,可以这样做:

 <cfset Application.Paths.Root = getDirectoryFromPath( getCurrentTemplatePath() ) /> 

Application.cfconApplicationStart事件内部


要确定运行CFML引擎的应用程序服务器所在的path,可以使用cfexecute访问shell命令,因此(可以考虑上面关于pwd / etc的讨论),可以这样做:

Unix的:

 <cfexecute name="pwd"/> 

对于Windows,创build一个包含文本@cdpwd.bat ,然后:

 <cfexecute name="C:\docume~1\myuser\pwd.bat"/> 

(使用cfexecutevariable属性来存储该值,而不是输出到屏幕。)

在cmd(Microsoft命令行shell)

你可以用%*得到脚本的名字(可能是相对于pwd)

这得到脚本的目录:

 set oldpwd=%cd% cd %0\.. set app_dir=%pwd% cd %oldpwd% 

如果你发现任何错误,你会。 然后请修复或评论。

我发布了https://github.com/gpakosz/whereami ,它解决了C中的问题,并给你:

  • 当前可执行文件的path
  • 当前模块的path(从共享库调用时,path与可执行文件不同)。

它在Windows上使用GetModuleFileNameW ,parsingLinux和Android上的/proc/self/maps ,并在Mac和iOS上使用_NSGetExecutablePathdladdr

Java的:

在所有系统上(Windows,Linux,Mac OS X)仅适用于我:

 public static File getApplicationDir() { URL url = ClassLoader.getSystemClassLoader().getResource("."); File applicationDir = null; try { applicationDir = new File(url.toURI()); } catch(URISyntaxException e) { applicationDir = new File(url.getPath()); } return applicationDir; } 

注意仅回答“20以上仅适用于Mac OSX:如果JAR可执行文件通过OSX JAR BUNDLER转换为”app“,则getClass()。getProtectionDomain()。getCodeSource()。getLocation();将不返回应用程序的当前目录,但会将应用程序的内部目录结构添加到响应中。应用程序的内部结构是/ theCurrentFolderWhereTheAppReside / Contents / Resources / Java / yourfile

也许这是Java中的一个小错误。 无论如何,一个人必须使用方法一或两个才能得到正确的答案,即使应用程序启动,例如通过位于不同文件夹或桌面上的快捷方式,两者都会提供正确的答案。

卡尔

SoundPimp.com