简体   繁体   中英

Java - get “program files” path

如何使用Java获取当前计算机的“Program Files”路径?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM