Recommendations for using C #: writing "temporary" files for download: a place in the application environment folder or temp folder

Basically, I wonder if there is a best practice when it comes to the next problem of downloading files, not just for temporary use, but ultimately for moving them to the application folder. I came across 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); 

Since some files are used at the time, I have no way to write the file directly to where it ends. He must go to the time domain ...

+4
source share
3 answers

Your first option is very nice. Its pretty clear and well-documented what is happening 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. Since Astander points to this answer , you probably want to use AppDomain.BaseDirectory, because dialogs can change the environment .CurrentDirectory

+8
source

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

This is the best choice because it will not raise SecurityExceptions or IOException, others may

+1
source

Are these winforms? The web? WPF? what? Why not just save it in the user profile of the application?

0
source

Source: https://habr.com/ru/post/1335929/


All Articles