Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C“最佳实践:写作”;“临时”;下载文件:放置在application';s环境文件夹或临时文件夹_C#_Temporary Files_Temporary Directory - Fatal编程技术网

C# C“最佳实践:写作”;“临时”;下载文件:放置在application';s环境文件夹或临时文件夹

C# C“最佳实践:写作”;“临时”;下载文件:放置在application';s环境文件夹或临时文件夹,c#,temporary-files,temporary-directory,C#,Temporary Files,Temporary Directory,基本上,我想知道在下载文件的问题上是否有一个最佳实践,不仅仅是为了临时使用,而是最终将其移动到应用程序文件夹。我面临着一些选择: //Option 1 - Random file String tempfile = Path.GetTempFileName(); WriteData(tempfile); File.Move(tempfile, Path.Combine(Environment.CurrentDirectory, filename); //Option 2 - Temp Path

基本上,我想知道在下载文件的问题上是否有一个最佳实践,不仅仅是为了临时使用,而是最终将其移动到应用程序文件夹。我面临着一些选择:

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

因为当时有些文件正在使用中,所以我没有选择将文件直接写到它最终要去的地方。它必须去一个临时区域…

这个winforms?网状物WPF?什么?为什么不将其存储在应用程序用户的配置文件中?

//选项4-临时应用程序路径+随机文件名

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

是最好的选择,因为它不会引发SecurityException或IOException,其他人可以

您的第一个选项非常好。这里发生的事情非常清楚,而且有很好的记录

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

除了
环境.CurrentDirectory
位之外。正如Astander在中指出的那样,您可能希望使用AppDomain.BaseDirectory,因为Environment.CurrentDirectory是环境的关键。我想应该说明的是,下载的文件对所有用户都是关键的。因此,是的,我可以将其下载到应用程序用户的配置文件文件夹(或临时文件夹),但最终必须将其移动到应用程序文件夹。这可能更好地作为注释,而不是回答“因为它不会引发SecurityException或IOException,其他人可以”。我不知道你为什么这么想。您不知道Settings.Default.DownloadFolder是什么,打开任何文件都可能导致安全或IOException。