Class StreamWriterOptions
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
StreamWriterOptions()
Initializes a new instance of the StreamWriterOptions class.
public StreamWriterOptions()
Remarks
The following table shows the initial property values for an instance of StreamWriterOptions.
| Property | Initial Value |
|---|---|
| AutoFlush | false |
| BufferSize | 1024 |
| Preamble | Keep |
| Encoding | DefaultEncoding |
| FormatProvider | InvariantCulture |
| NewLine | NewLine |
Properties
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.
public bool AutoFlush { get; set; }
Property Value
- bool
trueto force StreamWriter to flush its buffer; otherwise,false.
BufferSize
Gets or sets the size of the buffer.
public int BufferSize { get; set; }
Property Value
- int
The size of the buffer in bytes for the StreamWriter.
FormatProvider
Gets or sets the culture-specific formatting information used when writing data with the StreamWriter.
public IFormatProvider FormatProvider { get; set; }
Property Value
- IFormatProvider
An IFormatProvider that contains the culture-specific formatting information. The default is InvariantCulture.
NewLine
Gets or sets the line terminator string used by the StreamWriter.
public string NewLine { get; set; }
Property Value
- string
The line terminator string for the StreamWriter.