Monday, 12 March 2018

How to Give The FIle IO Permissions in AX 2012

Some Times we need to give the File IO Permissions to Read or Write the File.
These situations can be occur when we are running a file in Client and that need server permissions.
the following code will help you to do that.

public static void main(Args _args)
{

    Set permissionSet;
    WinAPIServer winAPIServer;
    str fileName;
    int size;
    RES_PROS_BatchJobProductExcelExport bjex;
    str                         strdatetime;
    date                        currentDate;
    ;

    currentDate = today();
    strdatetime = date2Str(currentDate,321,DateDay::Digits2,DateSeparator::Hyphen,DateMonth::Digits2,DateSeparator::Hyphen,DateYear::Digits4);
    fileName = "C:\\filenPath.xlsx";

    permissionSet =  new Set(Types::Class);
    //get file write permission for createFile
    permissionSet.add(new FileIoPermission(fileName,'w'));
    //get file read/write permission for getFileSize and closeHandle
    permissionSet.add(new FileIoPermission('','rw'));
    CodeAccessPermission::assertMultiple(permissionSet);

        //invoke protected APIs
    WinAPIServer::createFile(fileName);
    size = WinAPIServer::FileSize(fileName);

    bjex = new RES_PROS_BatchJobProductExcelExport();
    bjex.run();

}

the above code can help you.

No comments:

Post a Comment