Unable to copy Files in AX 2012

I have written a class to export the data into teh excel before exporting i need to copy old files into the new folder for this i have written the bellow code. but the file is not copying. When i run a job to check the file copy operation it is working but for class it is not working. and the class is running on the client. i have given the file IO permissions also but still no luck.

public static void main(Args _args)
{
SysExcelApplication xlsApplication;
SysExcelWorkBooks xlsWorkBookCollection;
SysExcelWorkBook xlsWorkBook;
SysExcelWorkSheets xlsWorkSheetCollection;
SysExcelWorkSheet xlsWorkSheet;
SysExcelRange xlsRange;
InventLocation inventLocation;
WinAPI winAPI;
int row = 1;
str fileName, newFileName;
str strdatetime;
date currentDate = today();
container companies;
UtcDateTime dateTime = DateTimeUtil::getSystemDateTime();
FileIoPermission fileIoPermission;

Set permissionSet;
WinAPIServer winAPIServer;

int size;

try
{
fileName =“G:\filepath\SITE.xlsx”;
//Initialize Excel instance
if (xSession::isCLRSession())
{
info(‘Running in a CLR session.’);
}
else
{
info(‘Running in an interpreter session.’);

//Determines the tier
if (isRunningOnServer())
{
info(‘Running on the AOS.’);
}
else
{
info(‘Running on the Client.’);
}
}
if(winAPI::fileExists(fileName))
{

strdatetime = DateTimeUtil::toStr(dateTime);
newFileName =“G:\FilePath\SITE_”+strdatetime+".xlsx";

permissionSet = new Set(Types::Class);
//get file write permission for createFile
permissionSet.add(new FileIoPermission(newFileName,‘w’));
//get file read/write permission for getFileSize and closeHandle
permissionSet.add(new FileIoPermission(’’,‘rw’));
CodeAccessPermission::assertMultiple(permissionSet);
//invoke protected APIs
//WinAPIServer::createFile(fileName);

if(winAPI::copyFile(fileName,newFileName,true))
{
info(“File Copied”);
}
else{
info(“File not copied”);
}

}
xlsApplication = SysExcelApplication::construct();
xlsWorkBookCollection = xlsApplication.workbooks();
xlsWorkBook = xlsWorkBookCollection.add();
xlsWorkSheetCollection = xlsWorkBook.worksheets();
xlsWorkSheet = xlsWorkSheetCollection.itemFromNum(1);

//Excel columns captions
xlsWorkSheet.cells().item(row,1).value(‘SITE_ID’);
xlsWorkSheet.cells().item(row,2).value(‘SITE_DESC’);
xlsWorkSheet.cells().item(row,3).value(‘MFG_SITE’);

companies = [“230”,“320”];
row++;

while select crossCompany: companies * from inventLocation where inventLocation.InventLocationType == InventLocationType::Standard
{
xlsWorkSheet.cells().item(row,1).value(inventLocation.InventLocationId);
xlsWorkSheet.cells().item(row,2).value(inventLocation.Name);
xlsWorkSheet.cells().item(row,3).value(‘NA’);
row++;

}
// WinApi::deleteFile(fileName);
if(WinApi::deleteFile(fileName))
{
info(“File deleted”);
}
xlsWorkbook.saveAs(fileName);
//xlsApplication.visible(true);
xlsApplication.quit();
xlsApplication.finalize();
info(strFmt(“Total number of rows retrived: %1”,row));
}
catch (Exception::Error)
{
info(strfmt(“Caught ‘Exception::Error’. %1”, time2str(timeNow() ,0,0)));
}

}

i wrote some info to check whether the file is copied are not the info log is as follows.

Running in an interpreter session.

Running on the Client.

File not copied //this is the copy operation output

File deleted

Total number of rows retrived: 294

Hi,

Try this