tags:

views:

455

answers:

4

I have a file I need to write to a temp location, what is the best place in Windows? This file needs to not move as I need to read it a couple of times and dispose it when I close the program.

Thanks

+14  A: 

use

Path.GetTempPath();

or

Path.GetTempFileName();

As commentator pointed out, GetTempFileName() is not threadsafe - but you can construct unique file names based on GUIDs.

tanascius
Note that GetTenpFileName might need to be called multiple time due to inherent race condition (something else might create the same file between return from GetTempFileName and opening the file).
Richard
you are right, thanks für the information
tanascius
+3  A: 

Use the Windows API function GetTempPath() from System.IO.Path (see MSDN)

using System.IO

...

myTempPath = Path.GetTempPath();

You should be aware that the filesystem might well change during your program's execution. Either the Temp path may change (unlikely, granted), or your temporary file might have been moved or deleted by the user.

Be prepared to check for its existence each time you access it and handle the case when it's not found gracefully.

Mark Pim
+5  A: 

The others beat me to it regarding

System.IO.Path.GetTempPath()

But you can also look into application data folder. That will allow you more control over your file, like the ability have 1 shared for all users or 1 pr user.

Application.CommonAppDataPath
Application.UserAppDataPath
EKS
A: 

If you need your application to write to a temporary location and work in partial trust, you'd need to look into IsolatedStorage.

Anderson Imes