Language agnostic 获取应用程序';s路径
我最近搜索了如何用Java获取应用程序的目录。我终于找到了答案,但我需要惊人的时间,因为寻找这样一个通用术语并不容易。我认为用多种语言编写一份如何实现这一目标的清单是一个好主意 如果你(不)喜欢这个想法,请随意投赞成票/反对票;如果你喜欢,请投稿 澄清:Language agnostic 获取应用程序';s路径,language-agnostic,path,Language Agnostic,Path,我最近搜索了如何用Java获取应用程序的目录。我终于找到了答案,但我需要惊人的时间,因为寻找这样一个通用术语并不容易。我认为用多种语言编写一份如何实现这一目标的清单是一个好主意 如果你(不)喜欢这个想法,请随意投赞成票/反对票;如果你喜欢,请投稿 澄清: 包含可执行文件的目录和当前工作目录之间有很好的区别(由Unix下的pwd给出)。我最初对前者感兴趣,但也可以发布确定后者的方法(澄清您的意思)。在Java中,有两种方法可以找到应用程序的路径。一种是使用System.getProperty: S
包含可执行文件的目录和当前工作目录之间有很好的区别(由Unix下的
pwd
给出)。我最初对前者感兴趣,但也可以发布确定后者的方法(澄清您的意思)。在Java中,有两种方法可以找到应用程序的路径。一种是使用System.getProperty
:
System.getProperty("user.dir");
另一种可能性是使用java.io.File
:
new java.io.File("").getAbsolutePath();
还有一种可能性是使用反射:
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
在.NET(C#,VB,…)中,您可以查询当前程序集
实例的位置
。但是,它附加了可执行文件的文件名。以下代码清理路径(使用System.IO
和使用System.Reflection
):
或者,您可以使用AppDomain提供的信息来搜索引用的程序集:
System.AppDomain.CurrentDomain.BaseDirectory
VB允许通过My
命名空间使用另一个快捷方式:
My.Application.Info.DirectoryPath
在bash中,“pwd”命令返回当前工作目录。Delphi 在Windows应用程序中:
Unit Forms;
path := ExtractFilePath(Application.ExeName);
在控制台应用程序中:
Unit Forms;
path := ExtractFilePath(Application.ExeName);
与语言无关,第一个命令行参数是完全限定的可执行文件名:
Unit System;
path := ExtractFilePath(ParamStr(0));
Python
path = os.path.dirname(__file__)
获取当前模块的路径。在Ruby中,以下代码段返回当前源文件的路径:
path = File.dirname(__FILE__)
Unix
在unix中,可以找到使用环境变量启动的可执行文件的路径。它不一定是绝对路径,因此您需要将当前工作目录(在shell中:pwd
)和/或路径变量与环境的第0个元素的值相结合
但是,该值在unix中是有限的,例如,可以通过符号链接调用可执行文件,并且只有初始链接用于环境变量。一般来说,如果unix上的应用程序将其用于任何有趣的事情(例如加载资源),那么它们就不是非常健壮。在unix上,通常使用硬编码的位置进行操作,例如在
/etc
中指定资源位置的配置文件。在Windows中,使用WinAPI函数。为模块句柄传入NULL以获取当前模块的路径。Libc在*nix类型环境中(也是Windows中的Cygwin):
#包括
char*getcwd(char*buf,size\u t size);
char*getwd(char*buf)//不赞成
char*get\u current\u dir\u name(void);
在CFML中,有两个用于访问脚本路径的函数:
getBaseTemplatePath()
getCurrentTemplatePath()
调用getBaseTemplatePath返回“基本”脚本的路径,即web服务器请求的路径。调用getCurrentTemplatePath返回当前脚本的路径,即当前正在执行的脚本的路径 这两个路径都是绝对路径,包含脚本的完整目录+文件名 要仅确定目录,请对结果使用函数
getDirectoryFromPath(…)
因此,要确定应用程序的目录位置,可以执行以下操作:
<cfset Application.Paths.Root = getDirectoryFromPath( getCurrentTemplatePath() ) />
对于Windows,创建一个包含文本@cd
的pwd.bat
,然后:
<cfexecute name="C:\docume~1\myuser\pwd.bat"/>
(使用
cfexecute
的variable
属性来存储值,而不是输出到屏幕。)在Java中调用
System.getProperty("user.dir")
及
返回当前工作目录
呼吁
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
返回包含当前类的JAR文件的路径,如果直接从文件系统运行,则返回生成当前类的CLASSPATH元素(path)
例如:
C:\MyJar.jar
cd
到C:\test\子目录java-jarc:\MyJar.jar
启动应用程序c:\eclipse\workspaces\YourProject\bin\
该路径不包括生成的类文件的包目录
获取应用程序目录的完整示例,如果直接从文件系统运行(例如,在调试时),则不使用.jar文件名或类文件的对应路径:
Objective-C Cocoa(Mac OS X,我不知道iPhone的具体情况):
在Tcl 当前脚本的路径:
set path [info script]
Tcl外壳路径:
set path [info nameofexecutable]
如果您需要其中任何一个目录,请执行以下操作:
set dir [file dirname $path]
获取当前(工作)目录:
set dir [pwd]
在.Net中,您可以使用
System.IO.Directory.GetCurrentDirectory
要获取应用程序的当前工作目录,在VB.NET中可以使用
My.Application.Info.DirectoryPath
要在PHP中获取exe的目录,请执行以下操作:
<?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).
?>
回答“20以上仅关于Mac OSX的说明:如果通过OSX JAR捆绑程序将JAR可执行文件转换为“应用程序”,则getClass().getProtectionDomain().getCodeSource().getLocation();将不会返回应用程序的当前目录,而是将应用程序的内部目录结构添加到响应中。此内部
set path [info script]
set path [info nameofexecutable]
set dir [file dirname $path]
set dir [pwd]
<?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).
?>
getApplicationInfo().dataDir;
Environment.getExternalStorageDirectory();
Environment.getExternalStoragePublicDirectory(String type);
set oldpwd=%cd%
cd %0\..
set app_dir=%pwd%
cd %oldpwd%
?App.Path
C:\Program Files\Microsoft Visual Studio\VB98
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;
}