Table of Contents

StreamWriterOptions Class

Definition

Namespace
Cuemon.IO
Assemblies
Cuemon.IO.dll
Source
src/Cuemon.IO/StreamWriterOptions.cs

Configuration options for StreamWriter.

public class StreamWriterOptions : StreamEncodingOptions, IParameterObject, IEncodingOptions
Inheritance
StreamWriterOptions
Implements
Inherited Members

Examples

StreamWriterOptions configures encoding, preamble handling, buffer size, format provider, and newline style when writing to streams via StreamWriter. This example sets up options with AutoFlush = true, BufferSize = 256, Encoding = EncodingOptions.DefaultEncoding, Preamble = PreambleSequence.Remove, FormatProvider = CultureInfo.InvariantCulture, and NewLine = "\n", then uses StreamFactory.Create with a writer delegate to format a string with Math.PI to two decimal places. The resulting stream is read back as a string via the decorator pattern. Console output displays the trimmed formatted output "Value: 3.14".

using System;
using System.Globalization;
using System.IO;
using Cuemon;
using Cuemon.IO;
using Cuemon.Text;

namespace Contoso.Reporting;

public sealed class StreamWriterOptionsExample
{
    public static void Run()
    {
        var options = new StreamWriterOptions
        {
            AutoFlush = true,
            BufferSize = 256,
            Encoding = EncodingOptions.DefaultEncoding,
            Preamble = PreambleSequence.Remove,
            FormatProvider = CultureInfo.InvariantCulture,
            NewLine = "\n"
        };

        using Stream stream = StreamFactory.Create(writer =>
        {
            string formatted = string.Format(options.FormatProvider, "Value: {0:F2}", Math.PI);
            writer.WriteLine(formatted);
        }, setup =>
        {
            setup.AutoFlush = options.AutoFlush;
            setup.BufferSize = options.BufferSize;
            setup.Encoding = options.Encoding;
            setup.Preamble = options.Preamble;
            setup.FormatProvider = options.FormatProvider;
            setup.NewLine = options.NewLine;
        });

        string output = Decorator.Enclose(stream).ToEncodedString(setup =>
        {
            setup.Encoding = options.Encoding;
            setup.Preamble = options.Preamble;
            setup.LeaveOpen = true;
        });

        Console.WriteLine(output.Trim());
    }
}

Constructors

Name Description
StreamWriterOptions()

Initializes a new instance of the StreamWriterOptions class.

Properties

Name Description
AutoFlush

Gets or sets a value indicating whether the StreamWriter will flush its buffer to the underlying stream after every call to the Write method.

BufferSize

Gets or sets the size of the buffer.

FormatProvider

Gets or sets the culture-specific formatting information used when writing data with the StreamWriter.

NewLine

Gets or sets the line terminator string used by the StreamWriter.