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は書き込みモードでのみ要求できます (AccessWriteに設定する必要があります)、新しいファイルを作成する場合 (ModeCreateまたはCreateNewに設定する必要があります)。 それ以外の場合、 FileStream コンストラクターは例外をスローします。

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

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

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

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

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

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

適用対象