Note
In the following code examples - for easier readability - exception handling
has been omitted.
public static CsvReader OpenRead(
string filePath,
char delimiter = ',',
Encoding? textEncoding = null,
bool isHeaderPresent = true,
CsvOpts options = CsvOpts.Default
)
Public Shared Function OpenRead (
filePath As String,
Optional delimiter As Char = ","C,
Optional textEncoding As Encoding = Nothing,
Optional isHeaderPresent As Boolean = true,
Optional options As CsvOpts = CsvOpts.Default
) As CsvReader
public:
static CsvReader^ OpenRead(
String^ filePath,
wchar_t delimiter = L',',
Encoding^ textEncoding = nullptr,
bool isHeaderPresent = true,
CsvOpts options = CsvOpts::Default
)
static member OpenRead :
filePath : string *
?delimiter : char *
?textEncoding : Encoding *
?isHeaderPresent : bool *
?options : CsvOpts
(* Defaults:
let _delimiter = defaultArg delimiter ','
let _textEncoding = defaultArg textEncoding null
let _isHeaderPresent = defaultArg isHeaderPresent true
let _options = defaultArg options CsvOpts.Default
*)
-> CsvReader
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.
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
*/
ArgumentNullException | filePath is null. |
ArgumentException | filePath is not a valid file path. |
ArgumentOutOfRangeException | delimiter is either the double quotes " or a line break character ('\r' or '\n'). |
IOException | I/O error. |