public sealed record CsvWriterOptions
Namespace
CsvHelper
Assembly
CsvHelper.dll
Inheritance
object CsvWriterOptions
Builds
net10.0 netstandard2.1 netstandard2.0 net47

Constructors

Properties

BufferSize

Added in v34.0.0
public int BufferSize { get; init; }

The size in bytes of the write buffer. Defaults to 64 KiB.

Property value

int

View source

CommentPrefix

Added in v34.0.0
public char? CommentPrefix { get; init; }

The character that starts the comment lines that CsvWriter.WriteComment(string) writes, such as #. Defaults to null: no comments. When it is set, a record whose first field starts with it has that field quoted (CsvMode.Rfc4180) or escaped (CsvMode.Escape), so that a reader with the same CsvReaderOptions.CommentPrefix does not skip the record; such a record throws InvalidOperationException in CsvMode.FixedWidth mode and is written as it is in CsvMode.NoEscape mode. Must be an ASCII character other than the separator, the quote (in CsvMode.Rfc4180 mode) and Escape (in CsvMode.Escape mode).

Property value

char?

View source

Escape

Added in v34.0.0
public char Escape { get; init; }

The escape character of CsvMode.Escape mode. Defaults to \. Must be an ASCII character other than space, tab and the separator.

Property value

char

View source

FalseValue

Added in v34.0.0
public string FalseValue { get; init; }

The text written for false. Defaults to False.

Property value

string

View source

InjectionCharacters

Added in v34.0.0
public string InjectionCharacters { get; init; }

The characters that make a text field a possible formula when it starts with one of them. Defaults to = @ + -, tab and CR, as OWASP recommends. Must be ASCII.

Property value

string

View source

InjectionEscape

Added in v34.0.0
public char InjectionEscape { get; init; }

The character that CsvInjection.Escape puts in front of a possible formula. Defaults to ', which Excel reads as "this cell is text". Must be an ASCII character other than CR, LF, the separator and the InjectionCharacters.

Property value

char

View source

Mode

Added in v34.0.0
public CsvMode Mode { get; init; }

How fields are delimited and escaped. Records end with NewLine in every mode.

  • CsvMode.Rfc4180 (the default): fields are quoted as Quoting says.
  • CsvMode.Escape: no quotes; Escape is written before every separator, escape character, CR and LF in a value. A record whose only field is empty is an empty line.
  • CsvMode.NoEscape: values are written as they are, so a value that contains the separator or a line break changes the fields or records that a reader sees. A record whose only field is empty is an empty line.
  • CsvMode.FixedWidth: no separators; each value is padded on the right with spaces to the width of its column (FieldWidths). A longer value, a value with a line break, or a field past the last column throws InvalidOperationException and the record is discarded.

Property value

View source

NewLine

Added in v34.0.0
public string NewLine { get; init; }

The line ending written after each record and comment: "\r\n" (the default, which RFC 4180 specifies), "\n" or "\r". Fields that contain CR or LF are still quoted or escaped whichever is chosen. In CsvMode.Escape mode with "\n", a record whose last value ends with a CR ends with CRLF, because a reader takes an LF right after an escaped CR as part of the value.

Property value

string

View source

NullValue

Added in v34.0.0
public string? NullValue { get; init; }

The text written for null: a null string, object or Nullable<T>, such as NULL or \N for a database import. Defaults to null: an empty field. Character and UTF-8 spans cannot be null, so an empty span is an empty field whatever this is. A reader with the same value in CsvReaderOptions.NullValues reads it back as null.

Property value

string?

View source

Separator

Added in v34.0.0
public char Separator { get; init; }

The field separator. RFC 4180 specifies a comma (the default). Another ASCII separator can be used (for example ; or tab); quoting still follows the RFC rules for that separator. Not used in CsvMode.FixedWidth mode.

Property value

char

View source

ShouldQuote

Added in v34.0.0
public CsvShouldQuote? ShouldQuote { get; init; }

Called for each field in CsvMode.Rfc4180 mode to decide whether to quote a field that does not need quotes, such as text fields but not numbers. A field that contains the separator, a quote, CR or LF is always quoted, and with CsvQuoting.Always every field is. Defaults to null. Writing is slower with a callback, which gets the text of every field. The parallel writes of CsvWriter call it on several threads at once.

Property value

View source

TrueValue

Added in v34.0.0
public string TrueValue { get; init; }

The text written for true. Defaults to True.

Property value

string

View source

WriteByteOrderMark

Added in v34.0.0
public bool WriteByteOrderMark { get; init; }

Whether to start the output with a UTF-8 byte order mark. Defaults to false.

Property value

bool

View source

Methods

For<T>()

Added in v34.0.0static
public static CsvWriterOptions For<T>()

The options for the files of T: the defaults with the settings of the type's CsvOptionsAttribute, or Default when it has none. Change the rest with a with expression, such as CsvWriterOptions.For<Order>() with { Quoting = CsvQuoting.Always }.

Type parameters

T
A CsvRecordAttribute type, or a type mapped by a CsvRecordMapAttribute.

Returns

CsvWriterOptions
The options, the same instance on every call.

Exceptions

CultureNotFoundException
The attribute names a culture that does not exist (or the app runs in globalization invariant mode).

View source