How to export csv file via x++ in Ax2012?


I am sure many of you out there might have already used this API within AX.
I had a requirement to spit out a .csv file from AX, Looping through Iventory Pricing and On-Hand Data.
And this code snippet from MSDN was handy for the purpose
Below sample shows example of how to output the data from CustTable.

CommaTextIo commaTextIo;
FileIOPermission permission;
CustTable custTable;
str fileName = @"C:\test.csv";
permission = new FileIOPermission(fileName,#io_write);
commaTextIo = new CommaTextIo(fileName,#io_write);
while select custTable