How to read and write text file using TextBuffer in Dynamics ax



Description:-

AX provides several ways of writing files. Depending upon scenario, you can use one. Apart from AX native filing, you can also use .NET interop to call .NET classes for reading/writing files. We will be showing few classes which are provided by AX for writing files.

TextBuffer

The TextBuffer class manages arbitrary text file content, and generates and manipulates text.
Saves the content of the TextBuffer object to a file.If the specified file already exists, it is overwritten without confirmation.

If an attacker can control input to File method, a security risk exists. Therefore, this method runs under Code Access Security. Calls to this method on the server require permission from the ExecutePermission class. Make sure that the user has development privileges by setting the security key to SysDevelopment on the control that calls this method.

Write Text Using TextBuffer

static void WriteTextFiletextBuffer(Args _args)
{
  BinData binData;
  TextBuffer textBuffer;
  str filepath;
  container confilter =['.txt','*.txt'];
  A_PurchaseOrder objA_PurchaseOrder;
  FileIoPermission permission;
  filepath = WinAPI::getSaveFileName(
    0,
    confilter,
    "",
    "Save As",
    "",
    "Untitled")
  #File
  ;
  permission = new FileIoPermission(filepath, #io_write);
  permission.assert();
  textBuffer = new textBuffer();
  textBuffer = new TextBuffer();
  //textBuffer.setText("A_PurchaseOrder Table Data");
  while select objA_PurchaseOrder
  {
    textBuffer.appendText(strfmt("%1\r | %2\r | %3\r | %4\r | %5\r\n",
       objA_PurchaseOrder.Purchase_ID,objA_PurchaseOrder.Vender_Code,
       enum2str(objA_PurchaseOrder.Status),
             date2str(objA_PurchaseOrder.Purchase_Date,123,DateDay::Digits2,DateSeparator::Hyphen,
             DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4),
    objA_PurchaseOrder.Purchase_Amount));
  }
  textBuffer.getText();
  binData = newBinData();
  binData.setStrData(textBuffer.getText());
  binData.saveFile(filepath);
  CodeAccessPermission::revertAssert();
}

Read text Using TextBuffer

static void ReadTextTextBuffer(Args _args)
{
    BinData binData;
    TextBuffer textBuffer;
    str filepath;
    containerconfilter =['.txt','*.txt'];
    A_PurchaseOrder objA_PurchaseOrder;
    FileIOPermission   permission;
    InteropPermission interopPermission;
    container           read;
    int Line;
    filepath = WinAPI::getOpenFileName(
        0,
        confilter,
        "",
        "Save As",
        "",
        "Untitled")
    #File
    ;
    interopPermission = new InteropPermission(InteropKind::ClrInterop);
    permission = new fileIOpermission(filepath,#io_read);
    permission.assert();
    textBuffer = new textBuffer();       
    textBuffer.fromFile(filepath);
    if(WinApi::fileExists(filepath))
    {
        //Line=textBuffer.numLines();
        textBuffer.getText();
        //info(strFmt("Lines = %1",Line));
        info(strFmt("Data \n%1",textBuffer.getText()));       
    }
    else
    {
        info("No file exists");
    }
}

Related Posts

Previous
Next Post »

Thanks for comments.....