FileHelpers - Before/After Write Event Handling

  • 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. Events And Notification
  3. Before/After Write Event Handling
Previous: Before/After Read Event Handling
Next: ErrorMode.ThrowException
Click button to edit
Events And Notification: Before/After Write Event Handling
Show how to implement write events
Input.txt
10249   TOMSP  05071996      11.61
10250   HANAR  08071996       0.00
10251   VICTE  08071996      41.34
10269   TOMSP  05071996      11.61
10230   HANAR  08071996      65.83
10151   VICTE  08071996      41.34
Report layout.cs
[FixedLengthRecord]
[IgnoreEmptyLines]
public class OrdersFixed
{
    [FieldFixedLength(7)]
    public int OrderID;

    [FieldFixedLength(8)]
    public string CustomerID;

    [FieldFixedLength(8)]
    public DateTime OrderDate;

    [FieldFixedLength(11)]
    public decimal Freight;
}

Run a record through engine using the write event to filter out unwanted details

RunEngine.cs
public override void Run()
{
    var engine = new FileHelperEngine<OrdersFixed>();

    var result = engine.ReadFile("Input.txt");

    //  add our filter logic.
    engine.BeforeWriteRecord += BeforeWriteEvent;
    engine.AfterWriteRecord += AfterWriteEvent;

    engine.WriteFile("output.txt", result);
}

private void BeforeWriteEvent(EngineBase engine, BeforeWriteEventArgs<OrdersFixed> e)
{
    //  We only want clients with large frieght values
    if (e.Record.Freight < 40)
        e.SkipThisRecord = true;
}

private void AfterWriteEvent(EngineBase engine, AfterWriteEventArgs<OrdersFixed> e)
{
    //  Hide a line
    if (e.Record.CustomerID.Trim() == "HANAR")
        e.RecordLine = "-- Insufficient Access";
}
output.txt
  10251 VICTE  08071996      41.34
-- Insufficient Access
  10151 VICTE  08071996      41.34

ImportantYou can use lambda expressions instead of event methods, for example:

RunEngineLambda.cs
public void RunLambda()
{
    var engine = new FileHelperEngine<OrdersFixed>();

    var result = engine.ReadFile("Input.txt");

    //  add our filter logic.
    engine.BeforeWriteRecord += (eng, e) => {
        if (e.Record.Freight < 40)
            e.SkipThisRecord = true;
    };
    engine.AfterWriteRecord += (eng, e) => {
        if (e.Record.CustomerID == "HANAR")
            e.RecordLine = "Insufficient Access";
    };

    engine.WriteFile("output.txt", result);
}
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs