CsvOpenRead(String, Char, Encoding, Boolean, CsvOpts) Method

Opens a CSV file for reading.

Definition

Namespace: FolkerKinzel.CsvTools
Assembly: FolkerKinzel.CsvTools (in FolkerKinzel.CsvTools.dll) Version: 2.0.1+2345335399184346d9b2cc992ed5c814406052c1
C#
public static CsvReader OpenRead(
	string filePath,
	char delimiter = ',',
	Encoding? textEncoding = null,
	bool isHeaderPresent = true,
	CsvOpts options = CsvOpts.Default
)

Parameters

filePath  String
File path of the CSV file.
delimiter  Char  (Optional)
The field separator character.
textEncoding  Encoding  (Optional)
The text encoding to be used to read the CSV file or null for UTF8.
isHeaderPresent  Boolean  (Optional)
true, to interpret the first line as a header, otherwise false.
options  CsvOpts  (Optional)
Options for reading the CSV file.

Return Value

CsvReader
A CsvReader that allows you to iterate through the CSV data.

Remarks

The method arguments can be determined automatically with CsvAnalyzer - or use OpenReadAnalyzed(String, Encoding, Header, Boolean, Int32).

When importing CSV data from Excel, the appropriate arguments can be determined with GetExcelArguments.

Example

  Note

In the following code examples - for easier readability - exception handling has been omitted.
C#
using FolkerKinzel.CsvTools;

namespace Examples;

internal static class DisableCachingExample
{
    public static void DisableCachingSideEffects(string filePath)
    {
        string[][] friends = [
                                ["Name", "City"],
                                ["Ingrid","Berlin"],
                                ["Joyce","New York"],
                                ["Horst","Hamburg"],
                                ["John","New York"],
                             ];
        friends.SaveCsv(filePath);

        Console.WriteLine("Which friends live in New York?: ");
        Console.Write("  Determine with cache enabled:  ");

        // Caching is enabled by default.
        using (CsvReader csv = Csv.OpenRead(filePath))
        {
            foreach (CsvRecord record in
                // ToArray() caches the results:
                csv.Where(x => x["City"].Span.Equals("New York", StringComparison.Ordinal)).ToArray())
            {
                Console.Write(record["Name"]);
                Console.Write(' ');
            }

            Console.WriteLine();
        }

        Console.Write("  Determine with cache disabled: ");

        // If you set the flag CsvOpts.DisableCaching and then try to cache the data,
        // you will get the wrong results:
        using (CsvReader csv = Csv.OpenRead(filePath,
                                            options: CsvOpts.Default.Set(CsvOpts.DisableCaching)))
        {
            foreach (CsvRecord record in
                // NOTE: Removing ".ToArray()" would cause the correct results.
                csv.Where(x => x["City"].Span.Equals("New York", StringComparison.Ordinal)).ToArray())
            {
                Console.Write(record["Name"]);
                Console.Write(' ');
            }
        }
    }
}
/*
Console Output:

Which friends live in New York?:
  Determine with cache enabled:  Joyce John
  Determine with cache disabled: John John
*/

Exceptions

ArgumentNullExceptionfilePath is null.
ArgumentExceptionfilePath is not a valid file path.
ArgumentOutOfRangeExceptiondelimiter is either the double quotes " or a line break character ('\r' or '\n').
IOExceptionI/O error.

See Also