windows 使用 Java 查找用户独立的 TEMP 目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1706982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find user independent TEMP directory with Java
提问by GHad
when running a Java application as service with the user 'LocalService', the temp directory ("java.io.tmpdir") points to 'c:/windows/temp' (for example).
当使用用户“LocalService”将 Java 应用程序作为服务运行时,临时目录(“java.io.tmpdir”)指向“c:/windows/temp”(例如)。
Running a Java application normally gives 'c:/documents and settings/user/local settings/temp' instead.
运行 Java 应用程序通常会提供“c:/documents and settings/user/local settings/temp”。
How can I determine the user independent temp folder 'c:/windows/temp' when my application runs normally?
当我的应用程序正常运行时,如何确定与用户无关的临时文件夹“c:/windows/temp”?
Thanks and greetings, GHad
谢谢和问候, GHa
采纳答案by GHad
You could:
你可以:
- as suggested by St Shadow, rely on some environment variable such as %WINDIR% or %SYSTEMROOT%, append "\temp" on the end, and use this.
or pass in this value to your app as a variable using a commandline argument to the JVM, e.g.
-Dmytempdir=%WINDIR%\temp
- 正如 St Shadow 所建议的,依赖于一些环境变量,例如 %WINDIR% 或 %SYSTEMROOT%,在末尾附加“\temp”,然后使用它。
或者使用命令行参数将此值作为变量传递给您的应用程序到 JVM,例如
-Dmytempdir=%WINDIR%\temp
As you mention, the user could change the values of either of these variables using System -> Environment Variables, but I don't think they'd have any affect on the system until a reboot anyway (...?).
正如您所提到的,用户可以使用 System -> Environment Variables 更改这些变量中的任何一个的值,但我认为它们不会对系统产生任何影响,直到重新启动(...?)。
Or...
或者...
- try and read the value from the registry using some nasty use of java.util.prefs.Preferences or something -- On my machine it looks like the value you're after is held in
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TEMP
.
- 尝试使用 java.util.prefs.Preferences 或其他东西从注册表中读取值——在我的机器上,你所追求的值看起来像是保存在
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\TEMP
.
This would likely have to be quite messy and I don't know if the Preferences class will get you access to the key you'd need to read. Again, there's not much you could do about the user changing the registry value either, if they really wanted to, but again I doubt it would have any affect until after a reboot, and would probably have an impact on more than just your app.
这可能会非常混乱,我不知道 Preferences 类是否能让您访问需要阅读的密钥。同样,如果用户真的想更改注册表值,您也无能为力,但我再次怀疑它会在重新启动之前产生任何影响,并且可能不仅会影响您的应用程序。
Cheers, --J
干杯,--J
回答by GHad
I'm not sure there is a 'clean' way of doing this.
我不确定是否有一种“干净”的方法可以做到这一点。
In this situation, I would probably create a directory specifically for the Java app and refer to it in a properties file.
在这种情况下,我可能会专门为 Java 应用程序创建一个目录并在属性文件中引用它。
回答by Lastnico
You can simply create your own temporary folder, add use the deleteOnExit()method to ensure this folder will be removed at the exit of your application.
您可以简单地创建自己的临时文件夹,添加使用deleteOnExit()方法以确保该文件夹将在您的应用程序退出时被删除。
回答by St.Shadow
Java system property java.io.tmpdir just point to system variable %TMP%. For normal user %TMP% points to %HOMEPATH%\temp, for other account - can be another path. You can try to use %SYSTEMROOT%\temp instead of java.io.tmpdir - %SYSTEMROOT% points to directory, where windows is installed.
Java 系统属性 java.io.tmpdir 只是指向系统变量 %TMP%。对于普通用户 %TMP% 指向 %HOMEPATH%\temp,对于其他帐户 - 可以是另一条路径。您可以尝试使用 %SYSTEMROOT%\temp 而不是 java.io.tmpdir - %SYSTEMROOT% 指向安装 Windows 的目录。