Class TempFileManager
Creates and manages a tempoary file ensuring that it is deleted when no longer required.
Inheritance
Implements
Inherited Members
Namespace: Sdl.FileTypeSupport.Framework
Assembly: Sdl.FileTypeSupport.Framework.Core.dll
Syntax
public class TempFileManager : IDisposable
Remarks
The temp file's file system readonly flag can also be set using ReadOnly and also the file can be locked using Locked to prevent it being deleted by the user or any disk space management software.
This class uses the FileJanitor class so it provides a convenient way to create and manage session-scope temporary files. Make sure that all objects needing access to the temp file have access to a corresponding TempFileManager object, that way when it is no longer referenced by any live objects and it gets garbage collected the temporary file is deleted.
The class also implements the IDisposable
interface, and deletes the
FileJanitor managed file in the Dispose()
implementation.
If the file for some reason cannot be deleted no error is raised.
Note that the .NET Framework does not guarantee that finalizers will be called, so there is always a possibility that the file does not get deleted.
The TempFileManager class also has a static method AutoDeleteNonLockedFiles() that can be called during applicaiton startup to delete any temp files that may not have been deleted previously.
Constructors
TempFileManager()
Creates a temp file with a random name and manages it. See TempFileManager(String, Boolean) for more details.
Declaration
public TempFileManager()
TempFileManager(String)
Creates the named temp file in a random temp directory or named sub-directory and manages it. See TempFileManager(String, Boolean) for more details.
Declaration
public TempFileManager(string filePath)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | A temp filename to create or empty for a random filename to be generated. |
TempFileManager(String, Boolean)
Creates a temp file and manages it.
Declaration
public TempFileManager(string filePath, bool lockFile)
Parameters
Type | Name | Description |
---|---|---|
System.String | filePath | A temp filename to create or empty for a random filename to be generated. |
System.Boolean | lockFile | If set to |
Remarks
If an empty file name is given then a random temp file is created. If only a file name is given then a temp folder is created and a file with the file name is created. If a full or partial path is given the then a temp folder with the last folder name in the path is created and a file with the file name is created within it.
Properties
CancelDelete
If true
then this managed file is not deleted when this instance is disposed.
Declaration
public bool CancelDelete { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Deleted
If true
then this managed file was sucessfully deleted.
Declaration
public bool Deleted { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
DeleteDirectoryIfEmpty
If true
a check will be made after the file
has been deleted, and if no other files exist in the
same directory that directory will also be deleted.
Declaration
public bool DeleteDirectoryIfEmpty { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
FilePath
Full path to the file to be deleted.
Declaration
public string FilePath { get; }
Property Value
Type | Description |
---|---|
System.String |
FileProcess
Can be set to a running process' System.Diagnostics.Process instance so that the managed temp file is deleted when the process exits.
Declaration
public Process FileProcess { get; set; }
Property Value
Type | Description |
---|---|
System.Diagnostics.Process |
Locked
Set to create (if it does not exist) and then to lock the temp file. The locked System.IO.FileStream is available from LockedFileStream.
Declaration
public bool Locked { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
LockedFileStream
Returns the locked System.IO.FileStream instace if the temp file was previously locked by setting Locked.
Declaration
public FileStream LockedFileStream { get; set; }
Property Value
Type | Description |
---|---|
System.IO.FileStream |
Remarks
The System.IO.FileStream is opened for read/write access and share read access.
ReadOnly
If set to true
the managed temp file's readonly file system flag is set.
Declaration
public bool ReadOnly { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Remarks
The file must exist or be subsequently locked by setting Locked for this property to have any effect.
Methods
AutoDeleteNonLockedFiles()
Deletes all TempFileManager managed tempoary files and tempoary folders that could not be deleted previously.
Declaration
public static void AutoDeleteNonLockedFiles()
Remarks
This public static method is designed to be called when the using applicatin starts up so that all tempoary files that have been created by a previous invocation of the applicaion that could not be automatically delted can be deleted when the using application restarts.
This also employs a locking mechanism so that if two instances of the application are launched only the first instance will delete temp files that were left from a previous instance of the applicaiton.
Dispose()
Deletes the file, if it exists.
Declaration
public void Dispose()
Dispose(Boolean)
Implementation of the recommended dispose protocol. Deletes the file if possible.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | true if this method is called from IDisposable.Dispose() and false if called from Finalizer |
Finalize()
Deletes the file, if possible.
Declaration
protected void Finalize()
TakeOverManagedFile(ref TempFileManager, TempFileManager)
This method handles the logic of handing over the management of a TempFileManager managed file to a new managed file
Declaration
public static void TakeOverManagedFile(ref TempFileManager managedFile, TempFileManager value)
Parameters
Type | Name | Description |
---|---|---|
TempFileManager | managedFile | |
TempFileManager | value |