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 。このファイルは、使用後に削除される場合があります。

一意の一時ファイル名を生成するディレクトリを指定するには、適切にオーバーロードされたコンストラクターを使用します。 また、コンストラクター オーバーロードを使用して、コレクションに追加されたファイルが、コレクションを破棄するとき、AddExtensionまたはDeleteメソッドが呼び出されたときにAddFile削除する必要があるかどうかを示すこともできます。

任意のディレクトリ内のファイルを、メソッドを使用するインスタンス TempFileCollectionAddFile 追加できます。

特定のファイル拡張子の一時ファイルの一意の名前を生成するには、生成するファイル名の拡張子を呼び出 AddExtension して指定します。 このメソッドは AddExtension 、プロパティで指定されたディレクトリ内の指定された拡張子のファイル名への完全パスで構成される文字列を TempDir 返します。 このメソッドは AddExtension 、ファイル名拡張子ごとに 1 つの一意のファイル名のみを返します。

AddFileメソッドとAddExtensionメソッドの両方にオーバーロードがあり、コレクションの破棄時またはメソッドの呼び出し時にファイルを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 によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

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)

IEnumerableIQueryable に変換します。

適用対象