Shimrod
Shimrod

Reputation: 3205

Is it possible to have a directory which only exists while the application is alive?

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 !

Upvotes: 2

Views: 252

Answers (4)

Ricardo Amores
Ricardo Amores

Reputation: 4687

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.

Upvotes: 2

LB2
LB2

Reputation: 4860

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.

Upvotes: 0

Crono
Crono

Reputation: 10478

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.

Upvotes: 3

TalkingCode
TalkingCode

Reputation: 13567

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.

Upvotes: 1

Related Questions