📜 ⬆️ ⬇️

Filehelpers

How often did you have to read data separated by, for example, a comma (CSV format) from a file, process it and do something with it in the future?

Of course, you can write the code yourself, but it will take time and will not always be effective.

FileHelpers - the module that will help you. It can synchronously or asynchronously read data from a file into an object. It can also write data back to the file with the specified separator. Can read data from the database. It still has a lot of any features that you can learn more on the official site of this module.
')

An example of asynchronous reading data from a file

Suppose you have the following file with the data separated by the symbol "|":
10248 | VINET | 04071996 | 32.38
10249 | TOMSP | 05071996 | 11.61
10250 | HANAR | 08071996 | 65.83
10251 | VICTE | 08071996 | 41.34
...............

You need to create a class that describes the file data structure:
[DelimitedRecord ( "|" )] // Define the delimiter
public class Orders
{
public int OrderID;

public string CustomerID;

[FieldConverter (ConverterKind.Date, "ddMMyyyy" )]
public DateTime OrderDate;

public decimal Freight;
} * This source code was highlighted with Source Code Highlighter .

And now we read the data asynchronously:
FileHelperAsyncEngine engine = new FileHelperAsyncEngine ( typeof (Orders));

engine.BeginReadFile ( "TestIn.txt" );

// The engine is IEnumerable
foreach (orders ord in engine)
{
// your code here
Console .WriteLine (ord.CustomerID);
}

engine.Close ();
* This source code was highlighted with Source Code Highlighter .

That's all.

Download

Download FileHelpers v2.0 from here .

Source: https://habr.com/ru/post/25885/


All Articles