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

1

9

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?

Erdman answered 1/12, 2009 at 17:32 Comment(0)
A
22

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();
}
Antisepticize answered 1/12, 2009 at 21:7 Comment(4)
Is there any reason to use one over the other?Arielariela
@Alex, the TextIo method only works on the "client". StreamWriter works on both "client" and "server" tiers.Perreira
@Perreira I'm pretty sure they both work on client/server tiers. The issue is usually if something is running on the server tier, and client-filepaths are used. I don't think there is a good reason to use StreamWriter except if you needed abilities that it provided over the other.Arielariela
What #File do?Ire

© 2022 - 2024 — McMap. All rights reserved.