To write to a csv in Microsoft Dynamics AX 2009 X++

//Macros
#File

//File Access Vars
FileIOPermission permission;
CommaTextIO commaio;
FilePath filePath = @'C:\MyFolder\MyOutput.csv';
;

//Set Write Permission For New File
permission = new FileIOPermission(filePath, #io_write);
permission.assert();

//Create CommaIO Object
commaio = new CommaTextIO(filePath, #io_write);

//You Might Want The Next Line To Go In A Loop
commaio.write('Field1', 'Field2', 'Field3', 'etc...etc');

//After Your Loop...
//This Line Isn't Always Needed But Releases
// The Lock On The Output File If It Is To Be Moved
commaio = null;

//Reset Write Access
CodeAccessPermission::revertAssert();