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

439 questions
6
votes
1 answer

FileHelpers quote and comma in fields

I have a csv file that I am parsing with FileHelpers and I have a situation where both a quote and a comma can appear in a field: Comma: 323,"PC","28/02/2014","UNI001","5000",0,"Return","Returned Goods,…
elector
  • 1,319
  • 4
  • 22
  • 43
6
votes
1 answer

FileHelpers - 'FieldConverter' is not valid on this declaration type

I'm trying to set the date format of a CSV file I'm reading from via the FieldConverter attribute but I'm receiving the following error - Attribute 'FieldConverter' is not valid on this declaration type. It is only valid on 'field'…
chickenbeef
  • 190
  • 9
6
votes
1 answer

How to make FileHelpers set the line number on each record?

Are there any chances to have the MultiRecordEngine set the line number for each record? I read the documentation and found that the Engine has a LineNumber property, but I couldn't find a way to use it. What I would like to…
Alexander
  • 453
  • 3
  • 11
6
votes
2 answers

Dynamically create a CSV file with FileHelpers

FileHelpers supports a feature called "RunTime Records" which lets you read a CSV file into a DataTable when you don't know the layout until runtime. Is it possible to use FileHelpers to create a CSV file at runtime in the same manner? Based on some…
Michael La Voie
  • 26,205
  • 14
  • 68
  • 92
6
votes
4 answers

how to handle null values for SQLDecimal datatype in FileHelper classes

I am trying to load a file using FileHelpers (like it already except for this one issue :P ) I have to save CSV file data into the database, and so am using a SqlDecimal datatype to store the decimal values of the CSV files.…
keepsmilinyaar
  • 967
  • 1
  • 9
  • 12
6
votes
1 answer

FileHelper escape delimiter

I am using FileHelper 2.0 for parsing my csv data. Is there any option that filehelper can properly handle escaped delimiter? That it can identify field as data and not as delimiter. Our csv format: escape comma (,) with \, Example data: name,…
broadband
  • 2,743
  • 2
  • 34
  • 61
6
votes
1 answer

Formatting properties with FileHelper

FileHelpers has a nice date converter for fields: [FieldConverter(ConverterKind.Date, "MM-dd-yyyy")] public DateTime MyDate; FieldConverter does not work with properties though. I have to deal with objects that use properties so I was looking for…
Jaguir
  • 3,553
  • 1
  • 17
  • 25
6
votes
1 answer

Using FileHelpers without a type

I have a CSV file that is being exported from another system whereby the column orders and definitions may change. I have found that FileHelpers is perfect for reading csv files, but it seems you cannot use it unless you know the ordering of the…
Ryan Amies
  • 4,732
  • 1
  • 18
  • 36
5
votes
2 answers

Multiple Date in FileHelpers how?

I am wondering how do I do multiple dates in filehelpers? It seems that you must specify every single format you are going to support (what kinda sucks...wish it could handle more of the basic ones). [FieldOrder(1),…
chobo2
  • 75,304
  • 170
  • 472
  • 780
5
votes
2 answers

Can the FileHelpers library write classes containing nullable fields as well as read them?

I'm using version 2.0 of the FileHelpers library which is documented as being able to handle .NET 2.0 Nullable types. I'm using the code given in the example from the documentation: [DelimitedRecord("|")] public class Orders { public…
nowhere_fast
  • 53
  • 1
  • 4
5
votes
2 answers

FieldConverter ConverterKind.Date "dd/MM/yyyy" exception

I try to read a csv file. my fifth record contans a date: 03/11/2008 This is a piece of my code: [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")] public DateTime datum_5; My code crashs on this: Result[] results=…
meersmans
  • 463
  • 5
  • 14
5
votes
1 answer

Optional quotation marks in filehelpers

My recent project requires me to read csv file. People direct me to filehelpers, since "you shouldn't reinvent the whell". However, the documentation really sucks, I can't seem to find a way to deal with optional quotation marks. Here is my…
Daniel Wu
  • 75
  • 7
5
votes
2 answers

How to define a default value for a field of a FileHelpers element class

I'm trying to load a CSV file (delims are ';' and quotes are '"'). I have successfully created everything (the wizard tool is awesome), but there's one thing that I can't find a solution for. Basically, I have an integer (System.Int32) column. In…
Paulius
  • 5,609
  • 7
  • 39
  • 47
5
votes
2 answers

How do I get the custom attribute value of a field?

I am using FileHelpers to write out fixed length files. public class MyFileLayout { [FieldFixedLength(2)] private string prefix; [FieldFixedLength(12)] private string customerName; public string CustomerName { set…
vijay
  • 595
  • 2
  • 11
  • 25
5
votes
2 answers

FileHelpers throws OutOfMemoryException when parsing large csv file

I'm trying to parse a very large csv file with FileHelpers (http://www.filehelpers.net/). The file is 1GB zipped and about 20GB unzipped. string fileName = @"c:\myfile.csv.gz"; using (var fileStream = File.OpenRead(fileName)) …
BowserKingKoopa
  • 2,632
  • 3
  • 31
  • 40
1
2
3
29 30