Charles Faiga
Charles Faiga

Reputation: 11763

Where is the best place to save temporary files in Windows

I am busy writing an application that runs under windows

Where is the correct place to save temporary files ?

Upvotes: 25

Views: 14628

Answers (7)

Yves
Yves

Reputation: 12507

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

Upvotes: -4

Steve Guidi
Steve Guidi

Reputation: 20198

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.

Upvotes: 42

Alan Moore
Alan Moore

Reputation: 1833

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.

Upvotes: 3

Michael
Michael

Reputation: 55445

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.

Upvotes: 12

Eric J.
Eric J.

Reputation: 150228

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.

Upvotes: 5

RichieHindle
RichieHindle

Reputation: 281865

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

Upvotes: 4

Martin Beckett
Martin Beckett

Reputation: 96177

In the temp directory?

Use GetTempPath or in a batch file %TEMP%

Upvotes: 6

Related Questions