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
3
votes
3 answers

Generic constraint for Constructor with string argument?

How to create a generic class that allows types which has constructor taking one string argument and implements ToString and implements Two functions as below. class Convert:ConverterBase where T:new() { public override…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
3
votes
1 answer

How do I fix a ConvertException while importing a csv file with the filehelpers library

I am processing a csv file that occasionally has text in an int field due to user input error. It is the first column and basically it is supposed to be a unique record id. I am catching the errors using engine.ErrorManager.ErrorMode =…
Breadtruck
  • 1,943
  • 5
  • 25
  • 39
3
votes
1 answer

FieldQuoted doesn't seem to work properly

I'm using FileHelpers 3.3 with VB .net. I tried to find an answer for a while, but all info I found referred to a behavior I'm not experiencing. So I begin to suspect a bug in FileHelpers. I have a very simple CSV: Description,Transaction…
Michoko
  • 33
  • 2
3
votes
5 answers

F# class constructor with no args --- error using filehelpers wtih f#

I followed the link of parse CSV using F# and filehelpers. got compiler error for the following code "The record class oneRow need a constructor with no args (public or private)" [] type oneRow= class …
user8321
  • 33
  • 3
3
votes
2 answers

Ignore NewLIne inside a field in FileHelpers C#

I am fairly new using FileHelpers Class for parsing the CSV. I have a file like this... "background","Info","Agent abcdefg =================== Context Tenant: {Vendor: 1, Customer: 719046046}","2,140.69","","7/11/2017 3:30 AM" I would…
Mahesh
  • 35
  • 7
3
votes
1 answer

How to read a stream from Azure File Store using Filehelpers

I'm trying to read a csv file from Azure File Store using FileHelpers. How does this work? I can connect to the azure file store and build a reference to te file. This is done by looping through the directories and files, in this example these are…
welleman
  • 33
  • 1
  • 4
3
votes
3 answers

Format DateTime to UTC with FileHelpers

I have a record that I write out to a CSV file using FileHelpers. I want the DateTime fields of the structure to be written out as the UTC date. I currently have the following formatter: [FieldConverter(ConverterKind.Date, "yyyy-MM-dd")] What do…
FunLovinCoder
  • 7,597
  • 11
  • 46
  • 57
3
votes
2 answers

FileHelpers - Error Converting '"""ID""' to type: 'Int32'

The IntValue1 in the csv file is a number without double quotes but when the system reads the row it adds double quotes at the beginning and also double quotes followed by semicolon at the end of the row to delimit the row. Because of this, the…
Sounouk
  • 95
  • 1
  • 12
3
votes
3 answers

How to extract data from an Excel file with C# / FileHelpers ExcelNPOIStorage

I am trying to extract data from an Excel sheet with Filehelpers ExcelNPOIStorage. Therefore I created a class : public static class UalExcelReader { public static UalShipmentRecord[] ReadInput(String pathToFile) { …
hot33331
  • 805
  • 11
  • 25
3
votes
1 answer

FileHelpers FieldConverter doesn't get called if value is whitespace

I am trying to parse a file using FileHelpers. I need to map fields to a KeyValuePair and for a few of these fields, there is a mapping if the string in the file is whitespace. However, my custom FieldConverter's FieldToString method does not seem…
bmay2
  • 382
  • 2
  • 4
  • 17
3
votes
1 answer

FileHelpers - extra column in file not raising an error when FieldQuoted attribute used

Using FileHelpers.dll version 3.0.1.0, with .net 4.0. Code to reproduce issue: create a file Accounts.txt like this: "AccountName","ExtraColumn" "MR GREEN ","abc" "MR SMITH ","def" c# Account class :…
Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
3
votes
2 answers

How do I write CSV file with header using FileHelpers?

I am using FileHelpers to write DataTable content into CSV file. Because of huge number of records in DataTable I chose to dump the result set as it is in DataTable into CSV file like below CommonEngine.DataTableToCSV(dt, filename) And the CSV has…
vijay
  • 635
  • 2
  • 13
  • 26
3
votes
0 answers

Null Value Found. You must specify a NullValueAttribute

I'm using the FileHelpers Library to import and process a spreadsheet. I'm having a problem with a couple of properties of the entity. I added the [FieldNullValue(null)] attribute to both properties but it had no effect. I still get the same error.…
Tim
  • 1,249
  • 5
  • 28
  • 54
3
votes
1 answer

Mapping a column that contains a space using FileHelpers library

I have a CSV and some of the headers have spaces or symbols in, eg. Country Name or Post/Zip Code I'm using some code like this to import it by mapping it to CatRecord objects: FileHelperEngine engine = new FileHelperEngine(); …
NibblyPig
  • 51,118
  • 72
  • 200
  • 356
3
votes
1 answer

how to create records for writing a csv file with FileHelpers

I am using Filehelpers to import my database from files. I am now working on the reverse process and am having trouble seeing how to create the records from my data objects. All the examples I can find show going from file -> table -> file I am…
Maggie
  • 1,546
  • 16
  • 27