TempFileCollection 类

定义

表示临时文件的集合。Represents a collection of temporary files.

public ref class TempFileCollection : IDisposable, System::Collections::ICollection
[System.Serializable]
public class TempFileCollection : IDisposable, System.Collections.ICollection
type TempFileCollection = class
    interface ICollection
    interface IEnumerable
    interface IDisposable
Public Class TempFileCollection
Implements ICollection, IDisposable
继承
TempFileCollection
属性
实现

示例

下面的示例演示如何使用TempFileCollectionAddExtension和和AddFile方法。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);
        }

    }

}
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
End Class

注解

TempFileCollection可用于生成唯一的文件名并跟踪文件列表。TempFileCollection can be used to generate unique file names and to keep track of a list of files. 这对于管理编译器生成ICodeCompiler的中间文件的列表 (有时在使用后会被删除) 很有用。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. 你还可以使用构造函数重载来指示在释放AddFile Delete集合或调用方法时是否应删除添加到集合的文件 ( AddExtension如果未指定, 如果使用或方法)。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.

TempFileCollection使用方法可以将任何目录中的文件添加到的实例中。AddFileA file in any directory can be added to an instance of TempFileCollection using the AddFile method.

若要为特定文件扩展名的临时文件生成唯一名称, 请调用AddExtension并指定要生成的文件名的扩展名。To generate a unique name for a temporary file of a particular file extension, call AddExtension and specify the extension of the file name to generate. 方法将返回一个字符串, 该字符串TempDir由属性指定的目录中指定扩展名的文件名的完整路径组成。 AddExtensionThe 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. 方法AddExtension只为每个文件扩展名返回一个唯一的文件名。The AddExtension method will only return one unique file name per file name extension.

和方法都具有重载, 可用于指定在释放Delete集合或调用方法时是否应删除文件。 AddExtension AddFileBoth 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.

Delete方法将删除该集合中的所有文件, 但标记为的文件除外。The Delete method will delete all the files in the collection except those that are marked to be kept.

属性指示基文件名的完整路径, 没有文件扩展名, 用于生成AddExtension方法返回的文件名。 BasePathThe 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.

备注

此类包含应用于所有成员的类级别的链接要求和继承要求。This class contains a link demand and an inheritance demand at the class level that applies to all members. 当直接调用方或派生类不具有完全信任权限时,将引发。SecurityExceptionA 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.

构造函数

TempFileCollection()

使用默认值初始化 TempFileCollection 类的新实例。Initializes a new instance of the TempFileCollection class with default values.

TempFileCollection(String)

使用指定的临时目录初始化 TempFileCollection 类的新实例,该临时目录默认情况下设置为在生成并使用临时文件后将这些文件删除。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.

TempFileCollection(String, Boolean)

使用指定的临时目录和指定的值初始化 TempFileCollection 类的新实例,该值指示默认情况下生成并使用临时文件后是保留这些文件还是将它们删除。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.

属性

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

获取或设置一个值,该值指示默认情况下在调用 Delete() 方法或释放集合时是否保留这些文件。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(String, Boolean)

将指定的文件添加到集合,并使用指定的值指示在释放集合或调用 Delete() 方法时是否保留该文件。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(String[], Int32)

从指定的索引处开始,将集合的成员复制到指定的字符串。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(Boolean)

释放由 TempFileCollection 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the TempFileCollection and optionally releases the managed resources.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Finalize()

在垃圾回收收回此对象之前尝试删除这些临时文件。Attempts to delete the temporary files before this object is reclaimed by garbage collection.

GetEnumerator()

获取一个枚举数,该枚举数可枚举集合的成员。Gets an enumerator that can enumerate the members of the collection.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

ICollection.CopyTo(Array, Int32)

从目标数组中指定的索引位置开始,将集合中的元素复制到该数组中。Copies the elements of the collection to an array, starting at the specified index of the target array.

ICollection.Count

获取集合中包含的元素数。Gets the number of elements contained in the collection.

ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。Gets a value indicating whether access to the collection is synchronized (thread safe).

ICollection.SyncRoot

获取可用于同步对集合的访问的对象。Gets an object that can be used to synchronize access to the collection.

IDisposable.Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

安全性

SecurityPermission
用于从TempFileCollection类派生。for deriving from the TempFileCollection class. 要求值: InheritanceDemand;PermissionSet: FullTrust.Demand value: InheritanceDemand; PermissionSet: FullTrust.

适用于