AX2012 / D365FnO – HOW TO READ A LINE FROM A TEXT FILE IN AX 2012 USING X++

D365fno-PostImage

In this blog post, I will show you how to read a line from a text file in AX 2012 using X++.

This can be useful for importing data, parsing logs, or processing configuration files.
 
Here is an example of how to do that:
static void ReadTextFromFile(Args _args)
{
    Filename                        filename = @'C:\ReadFrom\AX2012.txt';
    System.IO.StreamReader          reader;
    System.String                   line;
    InteropPermission               interopPermission;
    Str                             text;

    interopPermission = new InteropPermission(InteropKind::ClrInterop);
    interopPermission.assert();

    reader = new System.IO.StreamReader(filename,System.Text.Encoding::get_UTF8());
    line = reader.ReadLine();

    while (!System.String::IsNullOrEmpty(line))
    {
        text = line;
        info(strfmt("%1", text));
        line = reader.ReadLine();
    }

    reader.Close();
    reader.Dispose();
}
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.