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

By

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
http://msdn.microsoft.com/en-us/library/aa624902(v=ax.50).aspx
Below sample shows example of how to output the data from CustTable.

#File
CommaTextIo commaTextIo;
FileIOPermission permission;
CustTable custTable;
str fileName = @"C:\test.csv";
;
permission = new FileIOPermission(fileName,#io_write);
permission.assert();
commaTextIo = new CommaTextIo(fileName,#io_write);
while select custTable
{
commaTextIo.write(custTable.AccountNum,custTable.Name);
}
CodeAccessPermission::revertAssert();