Java path to Program Files folder in windows 64bit
-
27-06-2021 - |
質問
i`m trying to get the default Program Files folder on java. When I use:
System.getenv("ProgramFiles")
It returns "C:\Program Files" instead of "C:\Program Files (x86)"
I can add manually +(x86) but if the user will use 32bit system it will be the wrong folder.
解決
You should be using
System.getenv("ProgramFiles(X86)")
You can find the full reference on Wikipedia.
他のヒント
This is the correct answer for 32-bit Program Files directory
System.getenv("ProgramFiles(X86)")
However if a programmer is looking for the 64-bit Program Files folder but running a 32-bit JVM, System.getenv("ProgramFiles") will return "\Program Files (x86)\" as a side effect of 32-bit compatibility. In some cases, a programmer will still want the 64-bit ProgramFiles directory. This solution has its flaws, but will usually work...
System.getenv("ProgramFiles").replace(" (x86)", "")
Which is only marginally better then
System.getenv("SystemDrive") + "\Program Files"
-Tres
Possibly a try and catch.
try {
System.getenv("ProgramFiles(X86)");
}
catch (Exception e) {
System.getenv("ProgramFiles");
}
Maybe the Exception could be more specific but that general idea.