Questions tagged [filehelpers]

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:

Image

References

454 questions
4
votes
1 answer

Handling NEWLINE in DelimitedRecord with Filehelpers

Am using the excellent FileHelpers library to parse a number of different files. One of these files has (some) lines that look like this id|name|comments|date 01|edov|bla bla bla bla|2012-01-01 02|john|bla bla bla bla|2012-01-02 03|Pete|bla bla…
edosoft
  • 17,121
  • 25
  • 77
  • 111
4
votes
3 answers

Getting FileHelpers 2.0 to handle CSV files with excess commas

I am currently using the FileHelpers library (v2.0.0.0) to parse a CSV file. The CSV file is mapped to a class that has a handful of public properties, let's say there are N. The problem is that, by default, FileHelpers doesn't seem to correctly…
Dave
  • 14,618
  • 13
  • 91
  • 145
4
votes
1 answer

FileHelpers: Optional fields in non-quoted CSV

I am using FileHelpers to import data from a CSV file. Problem is, some versions of the CSV file have more fields than others. As such, I have marked the fields that are sometimes missing as being optional, but this does not seem to work as instead…
Martin Robins
  • 6,033
  • 10
  • 58
  • 95
3
votes
2 answers

C# regular expression trouble

Problem! I Have the following input (rules) from a flat file (talking about numeric input): Input might be a natural number (below 1000): 1, 10, 100, 999, ... Input might be a comma separated number surrounded by quotes (above 1000): "1,000",…
Eder
  • 1,874
  • 17
  • 34
3
votes
2 answers

How to skip over bad records

I am using File Helpers 2.9.9 and I am wondering how do I get it skip over bad records instead of it just crashing? object[] transactions = engine.ReadStream(textReader); // will crash if one record fails. I am also having trouble with the…
chobo2
  • 83,322
  • 195
  • 530
  • 832
3
votes
1 answer

Change FileHelpers EOL Character

I'm trying to parse 10GB of .dat files into something recognizable in .NET. The column delimiter is a '~' and the EOL is a '++EOL++'. I know how to handle the delimiter but I can't find an easy way to handle the '++EOL++' when there are no actual…
Scott
  • 189
  • 2
  • 10
3
votes
1 answer

Conditional record type with FileHelpers

I'm trying to parse a file using the Filehelpers library. My file looks like this: 000001,"A",123,456 000002,"B","ABC","XYZ" 000003,"B","DEF","XYZ" 000004,"B","HIJ","XYZ" My file contains rows that have different column definitions, where the…
Dav Evans
  • 4,031
  • 7
  • 41
  • 60
3
votes
2 answers

Passing Dynamic Loaded Type to SomeFunction

I am trying to pass the type of a dynamically loaded dll (it uses an interface, but I need the conectrete implementation of this) to a function and am missing something. var data = LoadAssemblyFromParamenter(pathToDataDll); Type dataType =…
user725819
  • 75
  • 1
  • 6
3
votes
1 answer

Different header and footer FileHelpers

I am using the FileHelpers library to create a txt document which contains a list of employees, the document needs to be created with fixed size fields, I need to add a header and footer with a different structure than the body, for example in the…
Andrés
  • 43
  • 5
3
votes
1 answer

Exporting DataTable using FileHelpers

I want to export the contents of a DataTable to a text delimited file using FileHelpers, is this possible? Here is what I have so far: // dt is a DataTable with Rows in it DelimitedClassBuilder cb = new DelimitedClassBuilder("MyClassName", "|",…
Nick Dat Le
  • 369
  • 4
  • 12
3
votes
1 answer

Getting values from Custom Field Attributes in C#

This morning I embarked on what I thought would be a quick exercise to use custom field attributes. Having tried many things and searching many examples (most involving class rather than field attributes), I'm officially stuck. My code is below. …
Glinkot
  • 2,924
  • 10
  • 42
  • 67
3
votes
1 answer

Problems with converting FixedLength format to to csv using same class definition

I defined both FixedLength and Delimited attributes on a single class. It reads the fixed length file fine but fails when i try to write out a csv. [FixedLengthRecord] [DelimitedRecord(",")] public class PCFFileHeader { …
3
votes
2 answers

Order of fields in a type for FileHelpers

I'm reading a simple CSV file with Filehelpers - the file is just a key, value pair. (string, int64) The f# type I've written for this is: type MapVal (key:string, value:int64) = new()= MapVal("",0L) member x.Key = key member x.Value =…
Bala
  • 781
  • 1
  • 6
  • 14
3
votes
1 answer

Output formatting with FileHelpers

I'm using FileHelpers to create fixed length files. In my model I have a double which needs to be outputted in a 0000.00 format. Is there anyway I can specify this with FileHelpers itself or do I need to change my model to a string and do a…
Shane Courtrille
  • 13,960
  • 22
  • 76
  • 113
3
votes
3 answers

FileHelper read to stream and download with web api

I have a list of objects which I want to download as csv file via Web Api. However when I test this with Swagger I get no csv download. What am I doing wrong? DailyReportContent contains 2 objects. This is my code so far: public IActionResult…
PitAttack76
  • 2,120
  • 5
  • 31
  • 44