2011-12-05 49 views
1

我正在閱讀使用FileHelpers庫的巨大文件。我想在閱讀下面的記錄之前更改RecordLine。FileHelpers在讀取記錄之前更改RecordLine

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

他們的在線幫助也說,這是可以改變

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

但它無法正常工作。我是否在做任何問題?

回答

0

我假設你正在設置事件?

engine.BeforeReadRecord += engine_BeforeReadRecord; 
2

假設RecordLine是一個字符串,你可以調用.Replace()函數,但是這個函數不會修改一個字符串內聯—它返回一個新的字符串。您需要將結果分配到某處:

if (e.RecordLine.Contains(@"\|")) 
    e.RecordLine = e.RecordLine.Replace(@"\|", ""); 
+0

感謝Joel,我早些時候嘗試過,它顯示e.RecordLine是隻讀的錯誤。 –

+0

@ RajanR.G在這種情況下,您需要同時使用我的答案和MarcosMeli's。另外 - 不需要if語句 - 如果字符串不包含文本,則替換調用將不起作用。 –

+0

謝謝喬爾,與您的決議的最新版本完美的作品。 –

相關問題