使用Java查找用户独立的TEMP目录

发布于 2024-08-10 21:09:17 字数 242 浏览 2 评论 0原文

当使用用户“LocalService”将 Java 应用程序作为服务运行时,临时目录(“java.io.tmpdir”)指向“c:/windows/temp”(例如)。

运行 Java 应用程序通常会给出“c:/documents and settings/user/local settings/temp”。

当我的应用程序正常运行时,如何确定用户独立的临时文件夹“c:/windows/temp”?

谢谢和问候, 甘德

when running a Java application as service with the user 'LocalService', the temp directory ("java.io.tmpdir") points to 'c:/windows/temp' (for example).

Running a Java application normally gives 'c:/documents and settings/user/local settings/temp' instead.

How can I determine the user independent temp folder 'c:/windows/temp' when my application runs normally?

Thanks and greetings,
GHad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少女七分熟 2024-08-17 21:09:17

您可以:

  • 按照 St Shadow 的建议,依赖某些环境变量,例如 %WINDIR% 或 %SYSTEMROOT%,在末尾附加“\temp”,然后使用它。
  • 或者使用 JVM 的命令行参数将此值作为变量传递给您的应用程序,例如

    -Dmytempdir=%WINDIR%\temp

正如您所提到的,用户可以更改其中任何一个的值
使用系统变量->环境变量,但我认为它们不会对系统产生任何影响,直到重新启动为止(...?)。

或者...

  • 尝试使用 java.util.prefs.Preferences 或其他东西从注册表中读取值 - 在我的机器上,您想要的值似乎保存在 HKEY_LOCAL_MACHINE\SYSTEM\ 中CurrentControlSet\Control\Session Manager\Environment\TEMP。

这可能会非常混乱,我不知道 Preferences 类是否能让您访问您需要读取的密钥。同样,如果用户确实想要更改注册表值,您也无能为力,但我再次怀疑它会在重新启动之前产生任何影响,并且可能不仅仅会对您的应用程序产生影响。

干杯,
--J

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

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 (...?).

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.

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.

Cheers,
--J

[浮城] 2024-08-17 21:09:17

我不确定是否有一种“干净”的方法来做到这一点。

在这种情况下,我可能会专门为 Java 应用程序创建一个目录,并在属性文件中引用它。

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.

绻影浮沉 2024-08-17 21:09:17

Java系统属性java.io.tmpdir只是指向系统变量%TMP%。
对于普通用户 %TMP% 指向 %HOMEPATH%\temp,对于其他帐户 - 可以是另一个路径。
您可以尝试使用 %SYSTEMROOT%\temp 而不是 java.io.tmpdir - %SYSTEMROOT% 指向安装 Windows 的目录。

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.

孤云独去闲 2024-08-17 21:09:17

您可以简单地创建自己的临时文件夹,添加使用 deleteOnExit() 方法以确保在应用程序退出时删除此文件夹。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文