Introduction
editMeziantou.Framework.TemporaryDirectory provides two types: TemporaryDirectory, a unique empty folder, and TemporaryFile, a unique file. Both are deleted when you dispose them, so a using statement is enough to clean up after a test, a build step or any work that needs scratch space on disk.
What you can do with it
- Create a scratch folder and the files and subfolders you need inside it, with paths that cannot escape it. See Temporary directories.
- Create a single temporary file, with a generated name, a name of your choice or at a path of your choice. See Temporary files.
- Pass them to any API: both types convert implicitly to
stringand toFullPath, and toDirectoryInfoorFileInfo.
Principles
- Unique names. Every folder and file name contains a timestamp and a GUID, so two instances never share a path, even when tests run in parallel.
- Private. On Linux and macOS, the folders are created so that only the current user can access them, and the files so that only the current user can read and write them.
- Deleted when disposed. Disposing deletes the folder and everything it contains, read-only files included. Disposing twice does nothing, so it cannot delete a folder that something else created at the same path afterward.
- Absolute paths. Paths are
FullPathvalues: absolute and normalized.
Namespace
TemporaryDirectory and TemporaryFile are in the Meziantou.Framework namespace, as FullPath is.
Next steps
- Getting started: install the package and create your first temporary folder.