Windows 中保存临时文件的最佳位置在哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1246942/
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
Where is the best place to save temporary files in Windows
提问by Charles Faiga
I am busy writing an application that runs under windows
我正忙着写一个在windows下运行的应用程序
Where is the correct place to save temporary files ?
保存临时文件的正确位置在哪里?
回答by Steve Guidi
If you are using .NET, please use Path.GetTempPath(). This will guarantee that you use the temporary directory assigned to the user that runs your application, regardless of where it is stored.
如果您使用 .NET,请使用Path.GetTempPath()。这将保证您使用分配给运行您的应用程序的用户的临时目录,而不管它存储在哪里。
If you browse the file system, you will notice that there are many "temp" directories:
如果您浏览文件系统,您会注意到有许多“临时”目录:
- ~\Temp
- ~\Windows\Temp
- ~\Users\userName\AppData\Local\Temp
- ~\温度
- ~\Windows\Temp
- ~\Users\userName\AppData\Local\Temp
... and many more. Some of these paths are OS-dependent, and won't be present on certain windows flavors. So, save yourself some time and hassle, and let the .NET framework figure out where the "temp" path is located.
... 还有很多。其中一些路径依赖于操作系统,并且不会出现在某些 Windows 版本中。因此,为自己节省一些时间和麻烦,让 .NET 框架找出“临时”路径所在的位置。
回答by Michael
Use GetTempPathand and possibly GetTempFileNameto determine where to put your temporary files. This is the most reliable, enduser-friendly, and future proof way to get a temporary location for files.
使用GetTempPath和可能的GetTempFileName来确定放置临时文件的位置。这是获取文件临时位置的最可靠、最终用户友好且面向未来的方法。
回答by Martin Beckett
回答by RichieHindle
Use the GetTempPathAPI, or the equivalent for your programming environment.
使用GetTempPathAPI 或适用于您的编程环境的等效 API。
回答by Eric J.
C:\Temp is NOT a good choice.
C:\Temp 不是一个好的选择。
If you are using .Net use code like this:
如果您使用 .Net,请使用如下代码:
string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string cfgFolder = Path.Combine(baseFolder, "MyAppName");
try
{
if (!Directory.Exists(cfgFolder))
{
Directory.CreateDirectory(cfgFolder);
}
}
catch { } // If no access, not much we can do.
to get a place for medium-term storage of app data, or Path.GetTempPath() for transient storage of data.
获取应用程序数据的中期存储位置,或 Path.GetTempPath() 用于临时存储数据。
回答by Alan Moore
It depends on the language you are using:
这取决于您使用的语言:
string tempFolder = System.IO.Path.GetTempPath();
will return you the appropriate folder in C# for instance.
例如,将在 C# 中返回适当的文件夹。
or, the environment variables TEMP or TMP if you must.
或者,如果必须,环境变量 TEMP 或 TMP。
回答by Yves
C:\Documents and Settings\username\Application Data\IsolatedStorage
C:\Documents and Settings\用户名\Application Data\IsolatedStorage