开发者

Java - get "program files" path

How开发者_如何学编程 can I get the current computer's "Program Files" path with Java?


Simply by calling System.getenv(...)

System.getenv("ProgramFiles");

Notice it will only work in Windows environments, of course :-)


System.getenv("%programfiles% (x86)"); 

for the 32-bit folder on 64-bit PC's.

Also, it works on any language in Windows Vista and newer. Calling either of the posted responses will work on any language installation, in fact.


For 32 bit use:

    System.out.println(System.getenv("ProgramFiles(X86)")); 

For 64 bit use:

    System.out.println(System.getenv("ProgramFiles")); 


Use the System.getenv() method:

public class EnvironmentVariableExample {

    public static void main(String[] args) {
        System.out.println(System.getenv("ProgramFiles"));
        System.out.println(System.getenv("MadeUpEnvVar"));
    }
}

If the variable doesn't exist, it will simply return null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜