FileStreamOptions.PreallocationSize プロパティ

定義

ファイルの初期割り当てサイズ (バイト単位)。 正の値は、通常のファイルが作成または上書きされている場合にのみ有効です (Create または CreateNew)。 負の値は使用できません。 それ以外の場合 (既定値の 0 の値を含む) は無視されます。 この値はヒントであり、強力な保証ではありません。 Web アセンブリ (WASM) と FreeBSD ではサポートされていません (値は無視されます)。 Windows、Linux、macOS の場合は、要求された割り当てサイズを埋めるためにディスク領域を事前に割り当てようとします。 それが不可能であることが判明した場合、操作は例外をスローします。 最終的なファイル長 (EOF) は、ファイルに書き込まれたバイト数によって決まります。

public:
 property long PreallocationSize { long get(); void set(long value); };
public long PreallocationSize { get; set; }
member this.PreallocationSize : int64 with get, set
Public Property PreallocationSize As Long

プロパティ値

ファイルの初期割り当てサイズをバイト単位で表す負以外の数値。

例外

が負の場合 value

次のコード例では、オブジェクトを操作FileStreamするときに を使用PreallocationSizeする方法を示します。

using System.IO;

public static class PreallocationSizeExample
{
    public static void Main()
    {
        string destinationPath = "destination.dll";

        var openForReading = new FileStreamOptions { Mode = FileMode.Open };
        using var source = new FileStream(typeof(PreallocationSizeExample).Assembly.Location, openForReading);

        var createForWriting = new FileStreamOptions
        {
            Mode = FileMode.CreateNew,
            Access = FileAccess.Write,
            PreallocationSize = source.Length // specify size up-front
        };
        using var destination = new FileStream(destinationPath, createForWriting);

        source.CopyTo(destination); // copies the contents of the assembly file into the destination file
    }
}
Imports System.IO

Module PreallocationSizeExample

    Sub Main()

        Dim destinationPath As String = "destination.dll"
        Dim openForReading = New FileStreamOptions With {
            .Mode = FileMode.Open
        }

        Using source = New FileStream(GetType(PreallocationSizeExample).Assembly.Location, openForReading)

            Dim createForWriting = New FileStreamOptions With {
                .Mode = FileMode.CreateNew,
                .Access = FileAccess.Write,
                .PreallocationSize = source.Length ' specify size up-front
            }

            Using destination = New FileStream(destinationPath, createForWriting)
                source.CopyTo(destination) ' copies the contents of the assembly file into the destination file
            End Using

        End Using

    End Sub

End Module

注釈

PreallocationSizeは書き込みモードでのみ要求できます ( をAccess に設定するWrite必要があります) と、新しいファイルを作成する場合は (Mode を または に設定するCreateCreateNew必要があります)。 それ以外の FileStream 場合、コンストラクターは例外をスローします。

オペレーティング システム、プラットフォーム、またはファイル システムが事前割り当てをサポートしていない場合、 PreallocationSize は無視されます。 これは、Web Assembly (WASM) と FreeBSD の場合です。

ディスク領域が不足しているか、ファイル システムが特定のサイズのファイル (FAT32 上の 5 GB ファイルなど) をサポートしていない場合は、例外がスローされます。

ファイルの長さは、ファイルに書き込まれたバイト数によって決まります。

ファイルが閉じられ、すべての割り当て済み領域が書き込まれるわけではない場合、残りの領域に対する処理はプラットフォームに依存します。 Windows では、この領域はファイル用に予約されなくなりました。 Linux や macOS などの他のプラットフォームでは、ファイルに割り当てられたままになります。

たとえば、ファイルには 2 GB が事前に割り当て済みで、書き込まれるのは 1 GB であるとします。 ファイルを閉じた後、ファイルの長さはすべてのオペレーティング システムで 1 GB になります。 Windows では、割り当てられたサイズも 1 GB ですが、Linux と macOS では、割り当てられたサイズは引き続き 2 GB です。

最初に事前割り当てされたものより多くを書き込むのは許容されます。 十分なディスク領域がある限り、操作は成功するはずです。

適用対象