CsvAnalyzeFile Method

Analyzes the CSV file referenced by filePath to get the appropriate method arguments for parsing.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static (CsvAnalyzerResult AnalyzerResult, Encoding Encoding) AnalyzeFile(
	string filePath,
	Encoding? defaultEncoding = null,
	Header header = Header.ProbablyPresent,
	int analyzedLines = 5
)

Parameters

filePath  String
File path of the CSV file.
defaultEncoding  Encoding  (Optional)
The text Encoding to be used if the CSV file has no byte order mark (BOM), or null to use UTF8 in this case. Use see cref="GetExcelArguments"/> to get the appropriate argument for this parameter when importing CSV data from Excel.
header  Header  (Optional)
A supposition that is made about the presence of a header row.
analyzedLines  Int32  (Optional)
Maximum number of lines to analyze in the CSV file. The minimum value is AnalyzedLinesMinCount. If the file has fewer lines than analyzedLines, it will be analyzed completely. (You can specify Int32.MaxValue to analyze the entire file in any case.)

Return Value

ValueTupleCsvAnalyzerResult, Encoding
A ValueTupleT1, T2, T3 containing the results of the analysis.

Remarks

The method performs a statistical analysis on the CSV file to find the appropriate parameters for reading the file. The result of the analysis is therefore always only an estimate, the accuracy of which increases with the number of lines analyzed. The analysis is time-consuming because the CSV file has to be accessed for reading.

The field delimiters COMMA (',', %x2C), SEMICOLON (';', %x3B), HASH ('#', %x23), TAB ('\t', %x09), and SPACE (' ', %x20) are recognized automatically.

This method also tries to determine the Encoding of the CSV file from the byte order mark (BOM). If no byte order mark can be found, defaultEncoding is used.

Exceptions

ArgumentNullExceptionfilePath is null.
ArgumentOutOfRangeException

header is not a defined value of the Header enum.

- or -

header is a combination of Header values.

ArgumentExceptionfilePath is not a valid file path.
IOExceptionI/O error.

See Also