Represents a collection of temporary files.
Assembly: System (in System.dll)
'Declaration <SerializableAttribute> _ <PermissionSetAttribute(SecurityAction.LinkDemand, Name := "FullTrust")> _ <PermissionSetAttribute(SecurityAction.InheritanceDemand, Name := "FullTrust")> _ Public Class TempFileCollection _ Implements ICollection, IEnumerable, IDisposable
Thetype exposes the following members.
|TempFileCollection||Initializes a new instance of the class with default values.|
|TempFileCollection(String)||Initializes a new instance of the class using the specified temporary directory that is set to delete the temporary files after their generation and use, by default.|
|TempFileCollection(String, Boolean)||Initializes a new instance of the class using the specified temporary directory and specified value indicating whether to keep or delete the temporary files after their generation and use, by default.|
|BasePath||Gets the full path to the base file name, without a file name extension, on the temporary directory path, that is used to generate temporary file names for the collection.|
|Count||Gets the number of files in the collection.|
|KeepFiles||Gets or sets a value indicating whether to keep the files, by default, when the Delete method is called or the collection is disposed.|
|TempDir||Gets the temporary directory to store the temporary files in.|
|AddExtension(String)||Adds a file name with the specified file name extension to the collection.|
|AddExtension(String, Boolean)||Adds a file name with the specified file name extension to the collection, using the specified value indicating whether the file should be deleted or retained.|
|AddFile||Adds the specified file to the collection, using the specified value indicating whether to keep the file after the collection is disposed or when the Delete method is called.|
|CopyTo||Copies the members of the collection to the specified string, beginning at the specified index.|
|Delete||Deletes the temporary files within this collection that were not marked to be kept.|
|Dispose||Releases the unmanaged resources used by the and optionally releases the managed resources.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Attempts to delete the temporary files before this object is reclaimed by garbage collection. (Overrides Object.Finalize.)|
|GetEnumerator||Gets an enumerator that can enumerate the members of the collection.|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns a string that represents the current object. (Inherited from Object.)|
|AsParallel||Overloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)|
|AsParallel||Overloaded. (Defined by ParallelEnumerable.)|
|AsQueryable||Overloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)|
|AsQueryable||Overloaded. (Defined by Queryable.)|
|Cast(Of TResult)||Overloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)|
|Cast(Of TResult)||Overloaded. (Defined by Enumerable.)|
|OfType(Of TResult)||Overloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)|
|OfType(Of TResult)||Overloaded. (Defined by Enumerable.)|
|ICollection.CopyTo||Infrastructure. Copies the elements of the collection to an array, starting at the specified index of the target array.|
|ICollection.Count||Infrastructure. Gets the number of elements contained in the collection.|
|ICollection.IsSynchronized||Infrastructure. Gets a value indicating whether access to the collection is synchronized (thread safe).|
|ICollection.SyncRoot||Infrastructure. Gets an object that can be used to synchronize access to the collection.|
|IDisposable.Dispose||Infrastructure. Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.|
|IEnumerable.GetEnumerator||Infrastructure. Returns an enumerator that iterates through a collection.|
can be used to generate unique file names and to keep track of a list of files. This can be useful to ICodeCompiler implementers when managing a list of compiler-generated intermediate files, which are sometimes deleted after use.
To specify a directory to generate unique temporary file names in, use an appropriately overloaded constructor. You can also use a constructor overload to indicate whether files added to the collection should, if not specified otherwise when using the AddFile or AddExtension methods, be deleted when the collection is disposed or the Delete method is called.
A file in any directory can be added to an instance of using the AddFile method.
To generate a unique name for a temporary file of a particular file extension, call AddExtension and specifiy the extension of the file name to generate. The AddExtension method will return a string consisting of a full path to a file name of the specified extension in the directory specified by the TempDir property. The AddExtension method will only return one unique file name per file name extension.
The Delete method will delete all the files in the collection except those that are marked to be kept.
This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands.
Imports System Imports System.CodeDom.Compiler Imports System.IO Class Program Shared Sub Main(ByVal args() As String) ' Create a directory in the current working directory. Directory.CreateDirectory("testDir") Dim tfc As New TempFileCollection("testDir", False) ' Returns the file name relative to the current working directory. Dim fileName As String = tfc.AddExtension("txt") Console.WriteLine(fileName) ' Name a file in the test directory. Dim file2Name As String = "testDir\test.txt" ' Add the file to the temp directory and indicate it is to be kept. tfc.AddFile(file2Name, True) Console.WriteLine(tfc.Count) ' Create and use the test files. Dim fs1 As FileStream = File.OpenWrite(fileName) Dim fs2 As FileStream = File.OpenWrite(file2Name) Dim sw1 As New StreamWriter(fs1) Dim sw2 As New StreamWriter(fs2) sw1.WriteLine("Test string") sw2.WriteLine("Test string") sw1.Close() sw2.Close() tfc.Delete() Console.WriteLine(tfc.Count) Try ' This call should succeed. File.OpenRead(file2Name) ' This call should fail. File.OpenRead(fileName) Catch e As FileNotFoundException Console.WriteLine(e.Message) End Try End Sub 'Main End Class 'Program