views:

991

answers:

7

Using the .net framework you have the option to create temporary files with

Path.GetTempFileName();

The MSDN doesn't tell us what happens to temporary files. I remember reading somewhere that they are deleted by the OS when it gets a restart. Is this true?

If the files aren't deleted by the OS, why are they called temporary? They are normal files in a normal directory.

A: 

No, this is not true. Basically, your app is responsible for cleaning up its own mess. If you don't, temporary files will accumulate over time.

Mihai Limbășan
+7  A: 

Based on the March files in my %tmp%, I'd say not.

Re why they are called temporary - because that is their expected usage. They aren't system files; they aren't application files, and they aren't user documents... they exist only to allow an app to do temporary processing (perhaps on large volumes of data), or often to pass data via IPC to another process. Hence they really are temporary.

You should aim to delete any temp files you create, fatal "kill" etc not withstanding. I often use "using" for this - I create a wrapper class - i.e.

sealed class TempFile : IDisposable { // formatted for space
    string path;
    public string Path {
        get {
            if(path==null) throw new ObjectDisposedException(GetType().Name);
            return path;
        }
    }
    public TempFile() : this(System.IO.Path.GetTempFileName()) { }

    public TempFile(string path) {
        if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
        this.path = path;
    }

    private void Dispose(bool disposing) {
        if (path != null) {
            try {
                File.Delete(path);
            } catch { } // best endeavours...
            path = null;
        }
    }
    public void Dispose() {
        GC.SuppressFinalize(this);
        Dispose(true);
    }
    ~TempFile() {
        Dispose(false);
    }
}
Marc Gravell
A: 

No, it lies in the responsibility of the software (read: the developer) that creates a temporary file to dispose of it.

Have a look in your own temp folder to see how well that works ;-)

Treb
+2  A: 

There is a FileOptions.DeleteOnClose option which might do what you want.

Here is a link to the MSDN page.

paul
A: 

They are called temporary because in most cases, the user can assume she can safely clean up the mess in the temp dirs... If not, in general, these files are locked anyway.

In general, these files should be short lived: create them, use them for whatever need you have, delete them on the spot. At worse, delete them when exiting the application.

Sometime, you cannot, eg. an archive manager or a VCS allowing to look at a file with an editor (or diff viewer, etc.), but closed before the editor (or not able to monitor the spawned process...).

PhiLho
+8  A: 
Cristi Diaconescu
A very nice article.
Pavel Minaev
A: 

In internet been read a lot of times about people dont want to use Path.GetTempFileName Because they says that could return a Already Existing file or when it changes the File Extension appears a conflict because a File With this Extension Exist, to solve problem make a file based on a GUID, well, this Function solves that Problem: Iterates until find a inexistent file name with an specific extension.

VB.net

Public Shared Function GetTempFileName(ByVal extensionWithDot As String) As String
    Dim tempFileName As String
    Do
        tempFileName = System.IO.Path.GetTempFileName
        If extensionWithDot IsNot Nothing Then
            tempFileName = tempFileName.Replace(System.IO.Path.GetExtension(tempFileName), extensionWithDot)
        End If
    Loop While System.IO.File.Exists(tempFileName)
    Return tempFileName
End Function

C#:

public static string GetTempFileName(string extensionWithDot)
{
    string tempFileName = null;
    do {
        tempFileName = System.IO.Path.GetTempFileName;
        if (extensionWithDot != null) {
            tempFileName = tempFileName.Replace(System.IO.Path.GetExtension(tempFileName), extensionWithDot);
        }
    } while (System.IO.File.Exists(tempFileName));
    return tempFileName;
}

Note: I use argument extensionWithDot because System.IO.Path.GetExtension returns with dot.

Braian Bressan
Review your english please.
Bogdan Gavril
Sorry, I will fix it.
Braian Bressan