How do i write a txt file using Microsoft Dynamics AX?

Marcelo picture Marcelo · Dec 1, 2009 · Viewed 32.5k times · Source

I want to write a txt file (just like i'd do in visual studio with c# using string writer and everything, with which i'm already very familiar)

what class and method do i use?

how does it work?

what's the X++ syntax?

Answer

user89974 picture user89974 · Dec 1, 2009

You can use the TextIo X++ class or the CLRInterop. Here are 2 X++ jobs to demonstrate both approaches.

static void Job_TextIO(Args _args)
{
    TextIo textIo;
    #File
    ;

    textIo = new TextIo(@"C:\textIOtest.txt", #IO_WRITE);
    textIo.write("Line 1\n");
    textIo.write("Line 2");
}


static void Job_StreamWriter(Args _args)
{
    System.IO.StreamWriter sw;
    InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);
    ;

    perm.assert();

    sw = new System.IO.StreamWriter(@"C:\test.txt");
    sw.WriteLine("Line 1");
    sw.WriteLine("Line 2");
    sw.Flush();
    sw.Close();
    sw.Dispose();

    CodeAccessPermission::revertAssert();
}