TempFileCollection 类

定义

表示临时文件的集合。

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

示例

下面的示例演示类 TempFileCollectionAddExtension 方法和 AddFile 方法的使用。

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 可用于生成唯一的文件名并跟踪文件列表。 当管理编译器生成的中间文件列表(有时在使用后删除)时,这对实现者非常有用 ICodeCompiler

若要指定要在其中生成唯一临时文件名的目录,请使用适当的重载构造函数。 还可以使用构造函数重载来指示是否应在使用 AddFileAddExtension 方法时指定添加到集合的文件(如果未指定)在释放 Delete 集合或调用方法时删除。

可以将任何目录中的文件添加到使用AddFile该方法的TempFileCollection实例。

若要为特定文件扩展名的临时文件生成唯一名称,请调用 AddExtension 并指定要生成的文件名的扩展名。 该方法 AddExtension 将返回一个字符串,其中包含属性指定的 TempDir 目录中指定扩展名的文件名的完整路径。 该方法 AddExtension 将仅返回每个文件扩展名的唯一文件名。

AddFileAddExtension方法都有重载,可用于指定在释放Delete集合或调用该方法时应删除文件。

该方法 Delete 将删除集合中的所有文件,但标记为保留的文件除外。

BasePath 属性指示基本文件名的完整路径(没有文件扩展名)用于生成方法返回 AddExtension 的文件名。

备注

此类包含应用于所有成员的类级别的链接需求和继承需求。 当即时调用方或派生类没有完全信任权限时,将引发 A SecurityException 。 有关安全需求的详细信息,请参阅 链接需求继承需求

构造函数

TempFileCollection()

使用默认值初始化 TempFileCollection 类的新实例。

TempFileCollection(String)

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

TempFileCollection(String, Boolean)

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

属性

BasePath

获取基文件名的完整路径,该基文件名不带文件扩展名,位于临时目录路径上,用于生成该集合的临时文件名。

Count

获取集合中文件的数目。

KeepFiles

获取或设置一个值,该值指示默认情况下在调用 Delete() 方法或释放集合时是否保留这些文件。

TempDir

获取用来存储临时文件的临时目录。

方法

AddExtension(String)

将具有指定文件扩展名的文件名添加到集合中。

AddExtension(String, Boolean)

将具有指定文件扩展名的文件名添加到集合,并使用指定的值指示是删除还是保留该文件。

AddFile(String, Boolean)

将指定的文件添加到集合,并使用指定的值指示在释放集合或调用 Delete() 方法时是否保留该文件。

CopyTo(String[], Int32)

从指定的索引处开始,将集合的成员复制到指定的字符串。

Delete()

删除此集合中没有标记为保留的临时文件。

Dispose(Boolean)

释放由 TempFileCollection 占用的非托管资源,还可以另外再释放托管资源。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Finalize()

在垃圾回收收回此对象之前尝试删除这些临时文件。

GetEnumerator()

获取一个枚举数,该枚举数可枚举集合的成员。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

从目标数组中指定的索引位置开始,将集合中的元素复制到该数组中。

ICollection.Count

获取集合中包含的元素数。

ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。

ICollection.SyncRoot

获取可用于同步对集合的访问的对象。

IDisposable.Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于