FileHelpers - Quick Start

  • 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)

Quick Start: Choose your file type

First, refer to the download / install page to try this on your machine.

  • Delimited
  • Fixed Length
  • Record by Record

You need to define a class that maps to the record in the source/destination file.
For this example we use this formatted file delimited by a ',':

Sample data
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
...............
Mapping Class
using FileHelpers;

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

	public string Name;

	public decimal Balance;

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

}
Create a FileHelperEngine for Read and Write
var engine = new FileHelperEngine<Customer>();

// To Read Use:
var result = engine.ReadFile("FileIn.txt");
// result is now an array of Customer

// To Write Use:
engine.WriteFile("FileOut.txt", result);
Use the resulting array
foreach (Customer cust in result)
{
    Console.WriteLine("Customer Info:");
    Console.WriteLine(cust.Name + " - " +
                      cust.AddedDate.ToString("dd/MM/yy"));
}

You need to define a class that maps to the record in the source/destination file.
For this example we use this formatted file which you can see is based on fixed lengths:

Sample data
01732Juan Perez           004350011052002
00554Pedro Gomez          123423006022004
00112Ramiro Politti       000000001022000
00924Pablo Ramirez        033213024112002
...............
Mapping Class
using FileHelpers;

[FixedLengthRecord()]
public class Customer2
{
	[FieldFixedLength(5)]
	public int CustId;

	[FieldFixedLength(20)]
	[FieldTrim(TrimMode.Right)]
	public string Name;

	[FieldFixedLength(8)]
	[FieldConverter(typeof(TwoDecimalConverter))]
	public decimal Balance;

	[FieldFixedLength(8)]
	[FieldConverter(ConverterKind.Date, "ddMMyyyy")]
	public DateTime AddedDate;

	// A custom converter
	internal class TwoDecimalConverter: ConverterBase
	{
		public override object StringToField(string from)
		{
			decimal res = Convert.ToDecimal(from);
			return res / 100;
		}

		public override string FieldToString(object from)
		{
			decimal d = (decimal) from;
			return Math.Round(d * 100).ToString();
		}
	}
}
Create a FileHelperEngine for Read and Write
var engine = new FileHelperEngine<Customer>();

// To Read Use:
var result = engine.ReadFile("FileIn.txt");
// result is now an array of Customer

// To Write Use:
engine.WriteFile("FileOut.txt", result);
Use the resulting array
foreach (Customer cust in result)
{
    Console.WriteLine("Customer Info:");
    Console.WriteLine(cust.Name + " - " +
                  cust.AddedDate.ToString("dd/MM/yy"));
}

You need to define a class that maps to the record in the source/destination file.
For this example we use this formatted file delimited by a ',':

Sample data
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
...............
Mapping Class
using FileHelpers;

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

	public string Name;

	public decimal Balance;

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

}
Create a FileHelperAsyncEngine for Read and Write
var engine = new FileHelperAsyncEngine<Customer>();

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

// Write
using(engine.BeginWriteFile("TestOut.txt"))
{
    foreach(Customer cust in arrayCustomers)
    {
       engine.WriteNext(cust);
    }
}
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs