TempFileCollection Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Represents a collection of temporary files.

Namespace:   System.CodeDom.Compiler
Assembly:  System (in System.dll)

System.Object
  System.CodeDom.Compiler.TempFileCollection

[SerializableAttribute]
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public class TempFileCollection : ICollection, IEnumerable, IDisposable

NameDescription
System_CAPS_pubmethodTempFileCollection()

Initializes a new instance of the TempFileCollection class with default values.

System_CAPS_pubmethodTempFileCollection(String)

Initializes a new instance of the TempFileCollection class using the specified temporary directory that is set to delete the temporary files after their generation and use, by default.

System_CAPS_pubmethodTempFileCollection(String, Boolean)

Initializes a new instance of the TempFileCollection 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.

NameDescription
System_CAPS_pubpropertyBasePath

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.

System_CAPS_pubpropertyCount

Gets the number of files in the collection.

System_CAPS_pubpropertyKeepFiles

Gets or sets a value indicating whether to keep the files, by default, when the Delete method is called or the collection is disposed.

System_CAPS_pubpropertyTempDir

Gets the temporary directory to store the temporary files in.

NameDescription
System_CAPS_pubmethodAddExtension(String)

Adds a file name with the specified file name extension to the collection.

System_CAPS_pubmethodAddExtension(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.

System_CAPS_pubmethodAddFile(String, Boolean)

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.

System_CAPS_pubmethodCopyTo(String[], Int32)

Copies the members of the collection to the specified string, beginning at the specified index.

System_CAPS_pubmethodDelete()

Deletes the temporary files within this collection that were not marked to be kept.

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the TempFileCollection and optionally releases the managed resources.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Attempts to delete the temporary files before this object is reclaimed by garbage collection.(Overrides Object.Finalize().)

System_CAPS_pubmethodGetEnumerator()

Gets an enumerator that can enumerate the members of the collection.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. Copies the elements of the collection to an array, starting at the specified index of the target array.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

This API supports the product infrastructure and is not intended to be used directly from your code. Returns an enumerator that iterates through a collection.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIDisposable.Dispose()

This API supports the product infrastructure and is not intended to be used directly from your code. Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.Count

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the number of elements contained in the collection.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether access to the collection is synchronized (thread safe).

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

This API supports the product infrastructure and is not intended to be used directly from your code. Gets an object that can be used to synchronize access to the collection.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

TempFileCollection 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 TempFileCollection 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.

Both the AddFile and AddExtension methods have overloads that allow you to specify whether the files should be deleted when the collection is disposed or the Delete method is called.

The Delete method will delete all the files in the collection except those that are marked to be kept.

The BasePath property indicates a full path to the base file name, without a file name extension, used to generate the file names returned by the AddExtension method.

System_CAPS_noteNote

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.

The following example shows the use of the TempFileCollection class and the AddExtension and AddFile methods.

using System;
using System.CodeDom.Compiler;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // Create a directory in the current working directory.
        Directory.CreateDirectory("testDir");
        TempFileCollection tfc = new TempFileCollection("testDir", false);
        // Returns the file name relative to the current working directory.
        string fileName = tfc.AddExtension("txt");
        Console.WriteLine(fileName);
        // Name a file in the test directory.
        string file2Name = "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.
        FileStream fs1 = File.OpenWrite(fileName);
        FileStream fs2 = File.OpenWrite(file2Name);
        StreamWriter sw1 = new StreamWriter(fs1);
        StreamWriter sw2 = 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 (FileNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }

    }

}

SecurityPermission

for deriving from the TempFileCollection class. Demand value: InheritanceDemand;PermissionSet: FullTrust.

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: