简体   繁体   English

C#最佳实践:编写“临时”文件以供下载:放在应用程序的环境文件夹或临时文件夹中

[英]C# Best Practices: Writing “temporary” files for download: Place in applicaion's environment folder or temp folder

Basically, I'm wondering if there is a best practice when it comes to the following issue of downloading files, not just for temporary use, but eventually to move them to the application folder. 基本上,我想知道下面的文件下载问题是否有最好的做法,不仅仅是临时使用,而是最终将它们移动到应用程序文件夹。 I'm faced with some options: 我面临一些选择:

//Option 1 - Random file
String tempfile = Path.GetTempFileName();
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

//Option 2 - Temp Path + Random file name
String tempfile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

//Option 3 - Temp Path + real file name
String tempfile = Path.Combine(Path.GetTempPath(), filename);
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

//Option 4 - Temp Application Path + Random file name
String tempfile = Path.Combine(Environment.CurrentDirectory, Settings.Default.DownloadFolder, Path.GetRandomFileName());
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

//Optioin 5 - Temp Application Path + file name
String tempfile = Path.Combine(Environment.CurrentDirectory, Settings.Default.DownloadFolder, filename);
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

Because some files are in use at the time I don't have the option of writing the file directly to where it ends up going. 因为当时正在使用某些文件,所以我无法直接将文件写入最终的位置。 It has to go to a temporary area... 它必须去临时区域......

Your first option is very nice. 你的第一个选择非常好。 Its pretty clear and well documented whats going on here. 它非常清晰,有据可查,记录在案。

//Option 1 - Random file
String tempfile = Path.GetTempFileName();
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

Except for the Environment.CurrentDirectory bit. 除了Environment.CurrentDirectory位。 As Astander points out in this answer you probably want to use AppDomain.BaseDirectory because the dialogs can change the Environment.CurrentDirectory 正如Astander在这个答案中指出的那样你可能想要使用AppDomain.BaseDirectory,因为对话框可以改变 Environment.CurrentDirectory

//Option 4 - Temp Application Path + Random file name //选项4 - 临时应用程序路径+随机文件名

String tempfile = Path.Combine(Environment.CurrentDirectory, Settings.Default.DownloadFolder, Path.GetRandomFileName());
WriteData(tempfile);
File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename);

Is the best choice, because it would not raise SecurityExceptions or IOException others can 是最好的选择,因为它不会引发SecurityExceptions或其他人可以的IOException

This winforms? 这个胜利? Web? 网? WPF? WPF? what? 什么? Why not just store it in the application user's profile? 为什么不将它存储在应用程序用户的配置文件中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM