FileStreamOptions.PreallocationSize 속성

정의

파일의 초기 할당 크기(바이트)입니다. 양수 값은 일반 파일을 만들거나 덮어쓰는 경우에만 적용됩니다(Create 또는 CreateNew). 음수 값은 허용되지 않습니다. 다른 경우(기본값 0 포함)에서는 무시됩니다. 이 값은 힌트이며 강력한 보장이 아닙니다. WASM(웹 어셈블리) 및 FreeBSD(값은 무시됨)에서 지원되지 않습니다. Windows, Linux 및 macOS의 경우 요청된 할당 크기를 채우기 위해 디스크 공간을 미리 할당하려고 합니다. 이것이 불가능한 것으로 판명되면 작업이 예외를 throw합니다. 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은 쓰기 모드(로 설정Write해야 합니다)Access에 대해서만 요청할 수 있으며, 새 파일을 만들 때(ModeCreate 또는 CreateNew로 설정해야 합니다). 그렇지 않으면 FileStream 생성자가 예외를 throw합니다.

운영 체제, 플랫폼 또는 파일 시스템이 사전 할당 PreallocationSize 을 지원하지 않는 경우 는 무시됩니다. WASM(웹 어셈블리) 및 FreeBSD의 경우입니다.

디스크 공간이 부족하거나 파일 시스템에서 지정된 크기의 파일(예: FAT32의 5GB 파일)을 지원하지 않는 경우 예외가 throw됩니다.

파일 길이는 파일에 기록된 바이트 수에 따라 결정됩니다.

파일이 닫혀 있고 할당된 공간이 모두 기록되지 않은 경우 나머지 공간에 발생하는 작업은 플랫폼에 따라 달라집니다. Windows에서 이 공간은 더 이상 파일용으로 예약되지 않습니다. Linux 및 macOS와 같은 다른 플랫폼에서는 파일에 할당된 상태로 유지됩니다.

예를 들어 파일에 대해 2GB가 미리 할당되었지만 1GB만 기록된다고 가정합니다. 파일을 닫은 후 파일 길이는 모든 운영 체제에서 1GB입니다. Windows에서 할당된 크기도 1GB이지만 Linux 및 macOS에서는 할당된 크기가 여전히 2GB입니다.

처음에는 사전 할당된 것보다 더 많은 것을 쓸 수 있습니다. 디스크 공간이 충분한 한 작업이 성공해야 합니다.

적용 대상