2

I am reading a huge file using FileHelpers library. I would like to change the RecordLine before reading the record like below.

   static void engine_BeforeReadRecord(object sender, BeforeReadRecordEventArgs<object> e)
    {
        if (e.RecordLine.Contains(@"\|"))
            e.RecordLine.Replace(@"\|", "");
    }

Their online help also says it is possible to change

Note: if you change the RecordLine the engine use the changed value
This can be useful in some cases but you must be carefull

But it is not working. Whether any issue in the way am i doing?

Marcos Meli
  • 3,468
  • 24
  • 29
Rajan R.G
  • 825
  • 1
  • 7
  • 10

3 Answers3

3

Assuming RecordLine is a string, you call the .Replace() function, but this function does not modify a string inline — it returns a new string. You need to assign the result somewhere:

if (e.RecordLine.Contains(@"\|"))
    e.RecordLine = e.RecordLine.Replace(@"\|", "");
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks Joel, I tried this earlier and it showed an error that e.RecordLine is read-only. – Rajan R.G Dec 06 '11 at 02:51
  • @RajanR.G In that case you need to use both my answer and MarcosMeli's. Also - there's no need for the if statement - if the string does not contain the text, the replace call will have no effect. – Joel Coehoorn Dec 06 '11 at 03:40
  • Thanks Joel, the latest version with your resolution works perfectly. – Rajan R.G Dec 06 '11 at 05:12
0

I assume that you're setting the event?

engine.BeforeReadRecord += engine_BeforeReadRecord;
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
0

With the last version of the library you can do that

http://www.filehelpers.net/download/

You can also use the INotifyRead interface:

http://www.filehelpers.net/example/EventsAndNotification/INotifyRead/

Marcos Meli
  • 3,468
  • 24
  • 29