tom
tom

Reputation: 5001

Where to write my temporary files to?

I have numerous byte[] representing pdf's. Each byte array needs to be loaded at the start of the application and shown as a thumbnail on my gui. So far I have managed to write the files to a temp location using:

System.IO.Path.GetTempPath();

Then using this path I get all pdf files, write them using

System.IO.File.WriteAllBytes(fileName, arrayOfPdfs[i]);

and then navigate to that directory, get all pdf files and turn them in to thumbnails in my app.

The thing is I only want the pdfs I have just put in the temp location only, so how else can I store the pdfs, or even where else can I store them so I know when I come to turn them in to thumbnails, the files I am reading are the ones I have just written? This is so I can be sure the user is only looking at the relevant pdfs that relate to their search on my system.

Thanks.

Upvotes: 15

Views: 15307

Answers (5)

Quintin Robinson
Quintin Robinson

Reputation: 82325

I would recommend in your users ApplicationData/LocalApplicationData folder provided by the OS for your app..

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Of course if the storage doesn't need to persist very long (really temporary) then you could just use the temp folder and create a folder inside of it to isolate your files.

Upvotes: 8

ChristopheD
ChristopheD

Reputation: 116117

Build a randomly named directory in the base temporary directory:

string directoryName = Path.GetRandomFileName();
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), directoryName));

Store your files in there.

Upvotes: 28

Steve Danner
Steve Danner

Reputation: 22148

Could you just create a subdirectory in the Temp path?

string dir = System.IO.Path.GetTempPath() + "\\<searchstring>;

Upvotes: 1

Refugee
Refugee

Reputation: 483

You can either:

  • Record the creation time of the first and last item, then only edit files which were created in that creation window
  • Move the files to a a holding folder, create the thumbnails and then move them to the folder that they're meant to be in, ensuring that the holding folder is empty at the end of the run

Upvotes: 0

jason
jason

Reputation: 241581

Use Path.GetTempFileName and keep track of the temporary files that you've allocated during the session. You should clean up when your program exits.

Upvotes: 0

Related Questions