Windows 临时目录详细信息 (Java)
我正在编写一个需要通用临时文件夹的程序。我正在尝试查找有关 Windows 临时文件夹的详细信息。我知道有两个路径 -
在 AppData\Local\Temp\ 下的每个用户目录中 这可能会根据 Windows 版本而改变?
在 Temp\ (C:\Windows\Temp) 下的系统文件夹中
我想知道 Windows 对这些文件到底做了什么。如果 Windows 从任一位置删除文件,它什么时候删除?我如何/应该使用这些目录进行编程?
编辑:我实际上有一个更大的问题 - 由于某个引擎我间接运行我的程序,它使用我在临时目录中创建的文件,我需要一个在路径中不使用空格字符的临时目录。 Windows 上的 Java System.getProperty("java.io.tmpdir") 为我提供用户目录中的临时值,在 XP 上该临时目录位于“文档和设置...”下 不好。有什么建议吗?这就是为什么我想知道 C:\Windows\Temp\ 目录...
I'm writing a program that needs a generic temp folder. I'm trying to find details about the Windows Temp folders. There are two paths that I know about -
In each user directory under AppData\Local\Temp\
This may change depending Windows version?In the system folder under Temp\ (C:\Windows\Temp)
I'm wondering exactly what Windows does to each of these. If Windows deletes files from either location, when does it do so? How can/should I use these directories for my programming?
EDIT: I have a bigger problem actually - Because of a certain engine I'm running indirectly with my program, which uses files I'm creating in a temp directory, I need a temp directory that doesn't use whitespace characters in the path. Java's System.getProperty("java.io.tmpdir") on Windows gives me the temp that's in the user directory, which on XP is under "Documents and Settings..."
Not good. Any suggestions? This is why I'm wondering about the C:\Windows\Temp\ directory...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这将为您提供 Java 中 Windows 临时目录的路径。
This will give you the path to the windows temp directory in Java.
不完全是。有一个用户和系统文件夹,其默认位置根据 Windows 版本、系统文件夹名称而变化,实际上在旧版本的 Windows 中,用户和系统情况都是相同的。但是,这些默认值可以被覆盖(它们位于我现在使用的系统上,它们与系统文件夹不在同一驱动器上)。
这些位置存储在系统变量中。一些框架(.NET、VB6,毫无疑问还有其他框架)为您提供了查找路径的便捷方法,而不必查找系统变量(例如.NET 中的 System.IO.Path.GetTempPath)。
Windows 不会为您清理临时文件夹(这就是为什么值得每隔几个月在您自己的计算机上清除旧文件),这取决于您是否玩得好。创建一个或多个不太可能使用任何其他软件正在使用的名称的文件(它们应该注意做同样的事情,因此任何名称都应该这样做,但假设系统上其他代码的最坏情况总是好的),并且完成后删除文件(或至少在应用程序退出时)。
在.NET中,System.IO.Path.GetTempFileName()将在临时区域中创建一个新文件并将其名称返回给您,这可以合理地保证不会与其他文件发生冲突,因此如果可以的话请使用该方法或类似的方法。
Not quite. There is a user and system folder, the default location of which varies according the windows version, system folder name, and indeed in older versions of windows was the same for both the user and system case. However, these defaults can be over-ridden (they are on the system I'm using now, where they aren't on the same drive as the system folder).
The locations are stored in system variables. Some frameworks (.NET, VB6 and no doubt others) give you convient ways to find the paths rather than having to look up the system variable (e.g. System.IO.Path.GetTempPath in .NET).
Windows does not clean up the temporary folder for you (which is why it's worth blasting out old files it every few months on your own machine), it's up to you to play nice. Create a file or files unlikely to step on the names any other software is using (they should take care to do the same, and so any name should do, but it's always good to assume the worse of other code on the system), and delete files when you're done (or on application exit at least).
In .NET System.IO.Path.GetTempFileName() will create a new file in the temp area and return the name of it to you, that is reasonably guaranteed not to conflict with others' so use that or similar methods if you can.
听起来您有两个程序需要共享临时文件,而其中一个程序绝对不希望路径名中包含空格。也许最简单的事情是:
因此在命令提示符下您可以执行以下操作:
希望有帮助。
It sounds like you have two programs that need to share temp files and one definitely doesn't want spaces in the path name. Probably the easiest thing to do is:
So at the command prompt you could do this:
Hope that helps.
要回答您的部分问题 - 如果您使用 .NET,您可以使用
System.IO
命名空间的Path.GetTempPath()
方法来获取临时目录。To answer part of your question - if you're using .NET, you can use the
Path.GetTempPath()
method of theSystem.IO
namespace to get the location of the temporary directory.我的 PC (XP SP3) 上定义的
%TEMP%
环境变量使用 DOS 风格的abcdef~1
目录名称 - 因此,如果您可以提取该变量,最终应该是一条没有空格的路径。例如
Start>Run>%TEMP%
将我带到C:\DOCUME~1\\LOCALS~1\Temp
但是,如果“超级用户” ' 摆弄该变量并将其指向其他地方,事情可能会失败。您可以查看这样的内容来检索8-char-and-no-空间路径。
The
%TEMP%
environment variable that's defined on my PC (XP SP3) uses the DOS-styleabcdef~1
directory names - hence, if you can pull that variable, you should end up with a path without spaces.e.g.
Start>Run>%TEMP%
takes me toC:\DOCUME~1\<user>\LOCALS~1\Temp
However, if a 'super-user' fiddles around with that variable and points it somewhere else, it's possible that things will fall over. You could look at something like this to retrieve the 8-char-and-no-spaces path.
使用此代码
use this code
你可以试试这个方法
you can try this way