views:

497

answers:

8

I am busy writing an application that runs under windows

Where is the correct place to save temporary files ?

+2  A: 

\Windows\system32 or \windows\system32\drivers

It's more fun that way.

samoz
funny... but only M$ does crazy things like that...
IPX Ares
unclear sarcasm not appreciated
Jason S
+2  A: 

In the temp directory?

Use GetTempPath or in a batch file %TEMP%

Martin Beckett
+15  A: 

If you are using .NET, please use Path.GetTempPath(). This will guarantee that you use the temporary directory assigned to the user that runs your application, regardless of where it is stored.

If you browse the file system, you will notice that there are many "temp" directories:

  • ~\Temp
  • ~\Windows\Temp
  • ~\Users\userName\AppData\Local\Temp

... and many more. Some of these paths are OS-dependent, and won't be present on certain windows flavors. So, save yourself some time and hassle, and let the .NET framework figure out where the "temp" path is located.

Steve Guidi
That - or use isolated storage, to which a user is always guaranteed to have write access!
marc_s
Better yet, just use Path.GetTempFileName() which will generate the file name as well, and return the full path to it (in the appropriate temp folder).
Pavel Minaev
+3  A: 

Use the GetTempPath API, or the equivalent for your programming environment.

RichieHindle
+3  A: 

C:\Temp is NOT a good choice.

If you are using .Net use code like this:

           string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

        string cfgFolder = Path.Combine(baseFolder, "MyAppName");


        try
        {
            if (!Directory.Exists(cfgFolder))
            {
                Directory.CreateDirectory(cfgFolder);
            }
        }
        catch { } // If no access, not much we can do.

to get a place for medium-term storage of app data, or Path.GetTempPath() for transient storage of data.

Eric J.
Why is it not a good idea to use `c:\Temp`? I'm not disagreeing with your point, I'm just curious as to your reasoning =)
David Thomas
C:\ might be a really small partition, and in many cases, normal (non-admin) users don't have write permissions on C:\ so they can't create C:\temp
marc_s
Moreover, who's to say that C:\temp even exists on non-English Windows?
Mark Rushakoff
+7  A: 

Use GetTempPath and and possibly GetTempFileName to determine where to put your temporary files. This is the most reliable, enduser-friendly, and future proof way to get a temporary location for files.

Michael
+3  A: 

It depends on the language you are using:

string tempFolder = System.IO.Path.GetTempPath();

will return you the appropriate folder in C# for instance.

or, the environment variables TEMP or TMP if you must.

Alan Moore
A: 

C:\Documents and Settings\username\Application Data\IsolatedStorage

Kombucha
Yes - but please use the documented Isolated Storage API and don't hardcode this path! On a system with a German version of Windows, this would be e.g. "C:\Dokumente\....." and if you hardcode this path, your app will break. FOLKS: IF THERE ARE APIs TO DO SOMETHING, **USE THEM**!!
marc_s