Click or drag to resize
FileHelperEngineT Class

One of the main classes of the library.

This engine is responsible to Read/Write the records at once from/to files or streams.

You can use the DelimitedFileEngine or the FixedFileEngine if you need to change some options at runtime

Inheritance Hierarchy

Namespace: FileHelpers
Assembly: FileHelpers (in FileHelpers.dll) Version: 3.1.5
Syntax
public class FileHelperEngine<T> : EventEngineBase<T>, 
	IFileHelperEngine<T>
where T : class

Type Parameters

T
The record type.

The FileHelperEngineT type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyEncoding
The encoding to Read and Write the streams. Default is the system's current ANSI code page.
(Inherited from EngineBase.)
Public propertyErrorManager
This is a common class that manages the errors of the library.
(Inherited from EngineBase.)
Public propertyErrorMode
Indicates the behavior of the engine when it finds an error. {Shortcut for )
(Inherited from EngineBase.)
Public propertyFooterText
The Read Footer in the last Read operation. If any.
(Inherited from EngineBase.)
Public propertyHeaderText
The Read Header in the last Read operation. If any.
(Inherited from EngineBase.)
Public propertyLineNumber
The current line number.
(Inherited from EngineBase.)
Public propertyOptions
Allows you to change some record layout options at runtime
(Inherited from EngineBase.)
Public propertyRecordType
Returns the type of records handled by this engine.
(Inherited from EngineBase.)
Public propertyTotalRecords
The total numbers of records in the last read/written file (only works with whole read/write).
(Inherited from EngineBase.)
Top
Methods
  NameDescription
Public methodCode exampleAppendToFile(String, IEnumerableT)
Append an array of records to the specified file.
Public methodCode exampleAppendToFile(String, T)
Append a record to the specified file.
Public methodGetFileHeader
Builds a line with the name of the fields, for a delimited files it uses the same delimiter, for a fixed length field it writes the fields names separated with tabs
(Inherited from EngineBase.)
Public methodCode exampleReadFile(String)
Read a file and return an array of the contained records.
Public methodCode exampleReadFile(String, Int32)
Read a file and return an array of the contained records.
Public methodReadFileAsDT(String)
Read the records of the file and fill a DataTable with them
Public methodReadFileAsDT(String, Int32)
Read the records of the file and fill a DataTable with them
Public methodCode exampleReadFileAsList(String)
Read a file and return an array of the contained records.
Public methodCode exampleReadFileAsList(String, Int32)
Read a file and return an array of the contained records.
Public methodCode exampleReadStream(TextReader)
Read a Stream and return an array of the contained records.
Public methodCode exampleReadStream(TextReader, Int32)
Read a Stream and return an array of the contained records.
Public methodReadStreamAsDT(TextReader)
Read the records of the stream and fill a DataTable with them
Public methodReadStreamAsDT(TextReader, Int32)
Read the records of the stream and fill a DataTable with them
Public methodCode exampleReadStreamAsList
Read a Stream and return an array of the contained records.
Public methodReadString(String)
Read a String and return an array of the contained records.
Public methodReadString(String, Int32)
Read a String and return an array of the contained records.
Public methodReadStringAsDT(String)
Read the records of a string and fill a DataTable with them.
Public methodReadStringAsDT(String, Int32)
Read the records of a string and fill a DataTable with them.
Public methodReadStringAsList(String)
Read a String and return an array of the contained records.
Public methodReadStringAsList(String, Int32)
Read a String and return an array of the contained records.
Public methodCode exampleWriteFile(String, IEnumerableT)
Write an array of records to the specified file.
Public methodCode exampleWriteFile(String, IEnumerableT, Int32)
Write the specified number of records from the array to a file.
Public methodCode exampleWriteStream(TextWriter, IEnumerableT)
Write an array of records to the specified Stream.
Public methodCode exampleWriteStream(TextWriter, IEnumerableT, Int32)
Write the specified number of records in the array to the Stream.
Public methodWriteString(IEnumerableT)
Write an array of records to an String and return it.
Public methodWriteString(IEnumerableT, Int32)
Write an array of records to an String and return it.
Top
Events
Remarks

You can set the ErrorMode of this class when found an error, and can retrieve them with the Errors property.

See in the Class Diagram and in the Quick Start Guide for more Info.

Or you can browse the Examples section for more code.

Engines Diagram:

Examples
This example shows a simple example of use of the library with a minimum of code:
using FileHelpers;

// First declare the record class

[Delimitedrecord("|")]
public class SampleType
{
    public string Field1;
    public int    Field2;
}


public void ReadExample()
{
    var engine = new FileHelperEngine<SampleType>());

    var records = engine.ReadFile("source.txt");

    // Now "records" array contains all the records in the
    // sourcefile and can be acceded like this:

    int sum = records[0].Field2 + records[1].Field2;
}


public void WriteExample()
{
    var engine = new FileHelperEngine<SampleType>();

    SampleType[] records = new SampleType[1];

    records[0] = new SampleType();

    records[0].Field1 = "Hello World";
    records[0].Field2 = 12;

    engine.WriteFile("destination.txt", records);

    // Now the file contains the created record in this format:
    // 
    // Hello World,12

}
See Also