CsvFileWriter

Ecng.IO

Class for writing to comma-separated-value (CSV) files.

Inherits: CsvFileCommon

Implements: IDisposable, IAsyncCommitable

Constructors

CsvFileWriter
public CsvFileWriter(Stream stream, Encoding encoding)
csvFileWriter = CsvFileWriter(stream, encoding)

Initializes a new instance of the CsvFileWriter class for the specified stream.

stream
The stream to write to
encoding
The text encoding.
CsvFileWriter
public CsvFileWriter(string path)
csvFileWriter = CsvFileWriter(path)

Initializes a new instance of the CsvFileWriter class for the specified file path.

path
The name of the CSV file to write to
CsvFileWriter
public CsvFileWriter(StreamWriter writer)
csvFileWriter = CsvFileWriter(writer)

Initializes a new instance of the CsvFileWriter class for the specified writer.

writer
StreamWriter

Properties

BaseStream
public override Stream BaseStream { get; }
value = csvFileWriter.BaseStream

Gets the underlying stream.

LineSeparator
public string LineSeparator { get; set; }
value = csvFileWriter.LineSeparator
csvFileWriter.LineSeparator = value

Gets or sets the character used to separate lines in the CSV file.

Methods

CommitAsync
public ValueTask CommitAsync(CancellationToken cancellationToken)
result = csvFileWriter.CommitAsync(cancellationToken)

Asynchronously commits the transaction by flushing data and propagating commit to the underlying stream if it supports transactions.

cancellationToken
CancellationToken

Returns: Task

DisposeManaged
protected override void DisposeManaged()
csvFileWriter.DisposeManaged()

Disposes the managed resources. Override this method to add custom clean up of managed resources.

Encode
public string Encode(string column)
result = csvFileWriter.Encode(column)

Encodes a column's value for output.

column
FlushAsync
public Task FlushAsync(CancellationToken cancellationToken)
result = csvFileWriter.FlushAsync(cancellationToken)

Asynchronously flushes the writer.

cancellationToken
CancellationToken

Returns: Task

Truncate
public void Truncate()
csvFileWriter.Truncate()

Truncates the underlying stream used by the CsvFileWriter by clearing its content.

WriteRowAsync
public ValueTask WriteRowAsync(IEnumerable<string> columns, CancellationToken cancellationToken)
result = csvFileWriter.WriteRowAsync(columns, cancellationToken)

Asynchronously writes a row of columns to the current CSV file.

columns
The list of columns to write
cancellationToken
CancellationToken

Returns: ValueTask