开发者

Java - get "program files" path

开发者 https://www.devze.com 2023-02-08 04:19 出处:网络
How开发者_如何学编程 can I get the current computer\'s \"Program Files\" path with Java?Simply by calling System.getenv(...)

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

精彩评论

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