C#最佳实践:写作与QUOT;临时"下载文件:放置在广告应用程式的环境文件夹或临时文件夹 [英] C# Best Practices: Writing "temporary" files for download: Place in applicaion's environment folder or temp folder

查看:160
本文介绍了C#最佳实践:写作与QUOT;临时"下载文件:放置在广告应用程式的环境文件夹或临时文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想知道如果有一个最好的做法,当涉及到下载的文件,而不只是临时使用的以下问题,但最终还是将它们移动到应用程序文件夹。我面临着一些选择:

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);

除了 Environment.CurrentDirectory 你可能想C $ C>位。作为Astander在这个答案使用AppDomain.BaseDirectory因为对话框可以改变的Environment.CurrentDirectory

Except for the Environment.CurrentDirectory bit. As Astander points out in this answer you probably want to use AppDomain.BaseDirectory because the dialogs can change the Environment.CurrentDirectory

这篇关于C#最佳实践:写作与QUOT;临时"下载文件:放置在广告应用程式的环境文件夹或临时文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆