FileHelpers - Read or Write Record by Record

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. QuickStart
  3. Read or Write Record by Record
Previous: Write Fixed File
Next: Autoproperties
Click button to edit
QuickStart: Read or Write Record by Record
Using the FileHelperAsynEngine to work record by record

If you have a source file like this, separated by a |:

Input.txt
1732,Juan Perez,435.00,11-05-2002
554,Pedro Gomez,12342.30,06-02-2004
112,Ramiro Politti,0.00,01-02-2000
924,Pablo Ramirez,3321.30,24-11-2002

You first declare a Record Mapping Class:

RecordClass.cs
[DelimitedRecord(",")]
public class Customer
{
    public int CustId;

    public string Name;

    [FieldConverter(ConverterKind.Decimal, ".")] // The decimal separator is .
    public decimal Balance;

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

}

Instantiate a FileHelperAsyncEngine and read or write files:

Example.cs
var engine = new FileHelperAsyncEngine<Customer>();

// Read
using(engine.BeginReadFile("Input.txt"))
{
    // The engine is IEnumerable
    foreach(Customer cust in engine)
    {
        // your code here
        Console.WriteLine(cust.Name);
    }
}


// Write

var arrayCustomers = new Customer[] {
    new Customer { CustId = 1732, Name = "Juan Perez", Balance = 435.00m,
                   AddedDate = new DateTime (2020, 5, 11) },
    new Customer { CustId = 554, Name = "Pedro Gomez", Balance = 12342.30m,
                   AddedDate = new DateTime (2004, 2, 6) },
};

using(engine.BeginWriteFile("TestOut.txt"))
{
    foreach(Customer cust in arrayCustomers)
    {
        engine.WriteNext(cust);
    }
}
Console
Juan Perez
Pedro Gomez
Ramiro Politti
Pablo Ramirez
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs