Click or drag to resize

DisposableTempFile Class

A class for managing a temporary file. The name is based off a Guid with an optional prefix or suffix. The file is deleted when this object is disposed.
Inheritance Hierarchy
SystemObject
  Atalasoft.Shared.IODisposableTempFile

Namespace:  Atalasoft.Shared.IO
Assembly:  Atalasoft.Shared (in Atalasoft.Shared.dll) Version: 11.0.0.0.0.297 (.NET 4.5.2, x86)
Syntax
public sealed class DisposableTempFile : IDisposable

The DisposableTempFile type exposes the following members.

Constructors
  NameDescription
Public methodDisposableTempFile
Creates a new temporary file which uses a Guid as the name.
Public methodDisposableTempFile(String)
Creates a new temporary file which uses a Guid as the name.
Public methodDisposableTempFile(String, String)
Creates a new DisposableTempFile which uses a Guid as the name.
Public methodDisposableTempFile(String, String, Boolean)
Initializes a new instance of the DisposableTempFile class.
Public methodDisposableTempFile(String, String, String)
Initializes a new instance of the DisposableTempFile class.
Public methodDisposableTempFile(String, String, String, Boolean)
Initializes a new instance of the DisposableTempFile class.
Top
Properties
  NameDescription
Public propertyFullName
Gets the full file name that was generated for this instance.
Public propertyIsCreated
Gets a value indicating whether the file has been created. Sets a value which if true, will create the file (If it hasn't already been created) else will delete the file (If it has been created).
Top
Methods
  NameDescription
Public methodCreate
Creates / overwrites the temporary file.
Public methodDelete
Delete the temporary file.
Public methodDispose
Releases all resources used by the DisposableTempFile
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize (Overrides ObjectFinalize.)
Public methodStatic memberFromName
Creates a DisposableTempFile from a known full path to a file.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodOpen
Opens the temporary file with the specified mode, access and share.
Public methodOpenRead
Opens the temporary file for reading. Requires that IsCreated is true.
Public methodOpenWrite
Opens the temporary file for writing. Requires that IsCreated is true.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Examples
Creates a temporary file, writes the byte '42' to it, proves that '42' was written and finally proves that the file was deleted when disposed.
String fullName = null;

// Prepares a new temporary .txt file located in a subdirectory called "MySubFolder" within the system temp directory.
using(DisposableTempFile tempFile = new DisposableTempFile("MySubFolder\\", ".txt"))
{
    // Stores the full name that was generated for the temp file.
    fullName = tempFile.FullName;

    // Creates the temp file.
    tempFile.IsCreated = true;

    // Writes the byte '42'
    using(Stream stream = tempFile.OpenWrite())
    {
        stream.WriteByte(42);
    }

    // Opens the same temp file that was just written for read.
    using(Stream stream = tempFile.OpenRead())
    {
        // This should pass... (Reads first byte, ensures it is 42).
        Debug.Assert(stream.ReadByte() == 42);
    }
} // Disposes tempFile.

// This should also pass... (Demonstrates that after TemporaryFile is disposed, the file is deleted).
Debug.Assert(File.Exists(fullName) == false);
See Also