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
屬性
實作

範例

下列範例示範如何使用TempFileCollection類別AddExtension和和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. 您也可以使用「函式多載」來指出加入集合的檔案是否應該在使用AddFileAddExtension方法時, 如果未指定, 則會在處置Delete集合或呼叫方法時刪除。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 AddFile使用方法, 將任何目錄中的檔案加入至的實例。A 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。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()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。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.

適用於