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?

Marcelo
  • 3,371
  • 10
  • 45
  • 76

1 Answers1

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();
}
  • 1
    Is there any reason to use one over the other? – Alex Kwitny Jan 06 '12 at 17:17
  • 1
    @Alex, the TextIo method only works on the "client". StreamWriter works on both "client" and "server" tiers. – rr789 Sep 29 '19 at 23:40
  • @rr789 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. – Alex Kwitny Oct 01 '19 at 16:29
  • What `#File` do? – Tomasz Filipek Oct 24 '22 at 07:59