C# 最佳实践:编写“临时”变量供下载的文件:放置在应用程序的环境文件夹或临时文件夹中
基本上,我想知道在以下下载文件问题时是否有最佳实践,不仅仅是为了临时使用,而是最终将它们移动到应用程序文件夹。我面临一些选择:
//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);
因为当时某些文件正在使用中,所以我无法选择将文件直接写入其最终去向的位置。它必须去一个临时区域...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的第一个选择非常好。这里发生的事情非常清楚且有据可查。
除了
Environment.CurrentDirectory
位。正如阿斯坦德在这个答案中指出的那样,您可能想使用 AppDomain.BaseDirectory 因为对话框可以更改Environment.CurrentDirectoryYour first option is very nice. Its pretty clear and well documented whats going on here.
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//选项 4 - 临时应用程序路径 + 随机文件名
是最好的选择,因为它不会引发 SecurityExceptions 或 IOException 其他人可以
//Option 4 - Temp Application Path + Random file name
Is the best choice, because it would not raise SecurityExceptions or IOException others can
这个winform?网络?世界和平基金会?什么?为什么不将其存储在应用程序用户的个人资料中呢?
This winforms? Web? WPF? what? Why not just store it in the application user's profile?