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

範例

下列範例示範 如何使用 TempFileCollection 類別和 AddExtensionAddFile 方法。

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 檔名。

注意

這個類別包含套用至所有成員之類別層級的連結需求和繼承需求。 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

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()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於