4

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...

michael
  • 14,844
  • 28
  • 89
  • 177

3 Answers3

8

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. As Astander points out in this answer you probably want to use AppDomain.BaseDirectory because the dialogs can change the Environment.CurrentDirectory

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
1

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

Is the best choice, because it would not raise SecurityExceptions or IOException others can

Alan Turing
  • 2,482
  • 17
  • 20
  • 1
    "because it would not raise SecurityExceptions or IOException others can". I don't know why you think this. You've no idea what Settings.Default.DownloadFolder is and also opening any file can lead to security or ioexceptions. – Conrad Frix Jan 18 '11 at 07:57
0

This winforms? Web? WPF? what? Why not just store it in the application user's profile?

John Batdorf
  • 2,502
  • 8
  • 35
  • 43
  • I guess it should be stated that the files being downloaded are key to all users. So yea, I could download it to the application user's profile folder (or temp folder), but eventually it HAS TO be moved to the application folder. – michael Jan 18 '11 at 04:19
  • 2
    This is probably better as a comment rather than an answer – Conrad Frix Jan 18 '11 at 04:19