2

I would like to use a temporary directory which would be automatically deleted once the application is stopped.

Does such a mechanism exist in the framework, or should I program it myself ?

Thanks !

Shimrod
  • 3,115
  • 2
  • 36
  • 56
  • possible duplicate of [Creating a temporary directory in Windows?](http://stackoverflow.com/questions/278439/creating-a-temporary-directory-in-windows) – Mithrandir Feb 21 '14 at 14:29
  • Mithrandir, I don't think this is a duplicate, I don't ask how to create a temp directory (which I can do) but if there is a built-in method to bind the directory's lifetime to the application's lifetime. – Shimrod Feb 21 '14 at 14:31
  • You could always put it on mono and use a docker container. That's about as temporary as it gets. – George Stocker Feb 21 '14 at 14:39
  • Do a search for *isolated storage*. – Eric Lippert Feb 21 '14 at 15:48

4 Answers4

3

There's nothing built-in that will do that. You can create the folder on startup and lock a file in it to prevent it's deletion by another process, but I'm pretty sure that's it.

If it's important that this folder not exists at all if the app isn't running then you'll want a service that monitors both the state of the app and folder. This way, should the app crash or the computer restarts, you'll be (reasonably) certain that the folder isn't accessible past either of these scenarios. Of course you will want to make your service start automatically on boot.

Crono
  • 10,211
  • 6
  • 43
  • 75
2

No built-in method for Directories exists as far as I know, but you can do easily mimic that behaviour by creating a disposable class and the using construct, which ensures that the folder will be deleted even if the app terminates unexpectedly:

public class TempFolder : IDisposable
{
    public string RootPath { get; private set; }

    public TempFolder()
    {
        RootPath = Path.GetTempPath();
    }

    public void Dispose()
    {
        Directory.Delete(RootPath, true);
    }
}

Then, in your application:

public static class MyApp {

public static void Main(string[] args)
{
    using(var tempFolder = new TempFolder())
    {

        // Do my stuff using tempFolder.RootPath as base path to create new files
    }

    // temporal directory will be deleted when we reach here
    // even if an exception is thrown! :)
}

}

Note that this is a simplistic approach; beware of locked files inside the temporally directory that may cause the Directory.Delete operation to fail

Also, some in some cases the Dispose method could not be called:

  • Some uncatchable exceptions like StackOverflowException and OutOfMemoryException
  • An uncatched exception is thrown in a different thread spawned by your application
  • The process is killed

BTW I'm using a similar approach to handle some NUnit tests that must operate over files, and it is working fine so far.

Ricardo Amores
  • 4,597
  • 1
  • 31
  • 45
  • 1
    If using this, should be aware that if program exists in unexpected ways (e.g. crash), it will not clean up directory. But this would work for typical cases. – LB2 Feb 21 '14 at 14:39
  • Actually it depends of the crash of the app. If the crash is caused by an uncatched .NET exception (which is the usual case), the dispose method is called anyway: http://stackoverflow.com/questions/8309877/if-an-exception-happens-within-a-using-statement-does-the-object-still-get-dispo – Ricardo Amores Feb 21 '14 at 14:40
  • 1
    Not on every exceptions. On StackOverflowException and OutOfMemoryException it won't be called. http://stackoverflow.com/questions/3304308/try-catch-finally-question – Crono Feb 21 '14 at 14:45
  • That being said, it's still an useful answer. +1 – Crono Feb 21 '14 at 14:45
  • 1
    @Ricky AH. Right, but even in unhandled .NET exception, you're assuming that it is on the same thread which may not be the case. An uncaught exception on another thread will not cause `Dispose` of this object. – LB2 Feb 21 '14 at 14:47
  • I was assuming a proper usage of threads, but point taken. I'll update the answer :) – Ricardo Amores Feb 21 '14 at 14:59
1

You should also keep in mind the application may be quit in a unusual way. Maybe even power down the computer. So the folder may already exists when you restart the program.

TalkingCode
  • 13,407
  • 27
  • 102
  • 147
0

Windows API has support for files to be created such that when the last handle to the file is closed, the file is deleted. However, I'm not sure such exists for a directory. Look into System.IO.File.CreateFile(); and FileOptions.DeleteOnClose for description. Also look into underlying Win32 API - perhaps you can adapt it to your needs.

LB2
  • 4,802
  • 19
  • 35