FileHelpers is a .net utility library to help applications manage flat file input and output.
FileHelpers is a utility library to help .NET framework applications manage flat file input and output. The bulk of the library lives in an assembly with a number of "engine" classes that are used to drive I/O along with attributes that are used to decorate classes in your application.
Your code uses these attributes to define records:
namespace OBG.FileHelpers.Tests
{
[FixedLengthRecord(FixedMode.ExactLength)]
internal class FixedRec1
{
[FieldFixedLength(10)]
[FieldAlign(AlignMode.Left)]
[FieldNullValue("n/a")]
[FieldTrim(TrimMode.Both)]
public String String10Field1;
[FieldFixedLength(10)]
[FieldConverter(ConverterKind.Date)]
public DateTime DateField2;
[FieldFixedLength(12)]
[FieldConverter(typeof(MoneyFieldConverter))]
public decimal MoneyField3;
}
}
To read or write files, then, one of the FileHelper engines works with the records you've defined to manipulate, format, and validate data:
var recs = new List<FixedRec1>();
recs.Add(new FixedRec1 { String10Field1 = "abc", DateField2 = DateTime.Today, MoneyField3 = 123.45M });
// Show truncation of field 1
recs.Add(new FixedRec1 { String10Field1 = "abcdefghijklmnopqrstuvwxyz", DateField2 = DateTime.Today, MoneyField3 = 123.45M });
// Show null translation of field 1
recs.Add(new FixedRec1 { DateField2 = DateTime.Today, MoneyField3 = 123.45M });
// Show illegal value for field3
recs.Add(new FixedRec1 { String10Field1 = "abc", DateField2 = DateTime.Today, MoneyField3 = -0.00001M });
// To write, use:
engine.WriteFile("FileOut.txt", recs.ToArray());
You can extend FileHelpers by constructing your own custom attributes, such as converters to handle formats not natively provided by FileHelpers.
FileHelpers is open source software released under the MIT.
Roslyn Analyzer
Best practices and quick fixes for the library:
References
- Product home page
- NuGet package
- Roslyn Analyzer.
- Source is available on GitHub.
- Contributors Guide.