FileHelpers - INotifyWrite Interface

  • 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. INotifyWrite Interface
Previous: INotifyRead Interface
Next: Before/After Read Event Handling
Click button to edit
Events And Notification: INotifyWrite Interface
Get Before/After Write events with the INotifyWrite interface
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
    :INotifyWrite
{
    [FieldFixedLength(7)]
    public int OrderID;

    [FieldFixedLength(8)]
    public string CustomerID;

    [FieldFixedLength(8)]
    public DateTime OrderDate;

    [FieldFixedLength(11)]
    public decimal Freight;

    public void BeforeWrite(BeforeWriteEventArgs e)
    {  
        //  We only want clients with large frieght values
        if (Freight < 40)
            e.SkipThisRecord = true;
    }

    public void AfterWrite(AfterWriteEventArgs e)
    {
        //  Hide a line
        if (CustomerID.Trim() == "HANAR")
            e.RecordLine = "-- Insufficient Access";
    }
}

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

RunEngine.cs
var engine = new FileHelperEngine<OrdersFixed>();

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

engine.WriteFile("output.txt", result);
output.txt
  10251 VICTE  08071996      41.34
-- Insufficient Access
  10151 VICTE  08071996      41.34
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs