web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

file access issue

(0) ShareShare
ReportReport
Posted on by

Hello,

Deletion of the file is throwing error that can't access file or other process is using it.

Below are the steps i am doing.

1) I am generating PDF file from SSRS report and it gets placed in temp folder on the AOS server.

2) I am converting the file to MemoryStream

3) calling a method which processes the Memorystream.

4)  deleting the file from temp folder.

Below is the code  that starts from step 2 assuming step 1 is already performed.

-----------------------------------------------------------------------------

new FileIOPermission(tempFileNameWithPath, #io_read).assert();

if (WinAPIServer::fileExists(tempFileNameWithPath))
{

reportIsEmpty = (WinAPIServer::fileSize(tempFileNameWithPath) == 0);

// starts creating memorystream

fileStream = System.IO.File::OpenRead(tempFileNameWithPath);
memoryStream = new System.IO.MemoryStream();
size = fileStream.get_Length();
memoryStream.SetLength(size);
fileStream.Read(memoryStream.GetBuffer(), 0, size); //write to MemoryStream

// call the method to process memorystream
}

CodeAccessPermission::revertAssert();

// Some other business logic goes here which has nothing to do with file or memorystream created above.

new FileIOPermission(tempFileNameWithPath, #io_write).assert();
WinAPIServer::deleteFile(tempFileNameWithPath);
CodeAccessPermission::revertAssert();

I am getting error when i am trying to delete the file shown in green section above. If i comment out red color code, everything works fine and file gets deleted successfully. but somehow permission is not getting reverted eventhough i have used revertAssert() statement (Pink color) before deleting file.

 

I believe file is getting locked when memorystream is getting generated and never gets unlocked.

Can anyone help me how to get rid of this lock?

 

 

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Rudi Hansen Profile Picture
    4,075 on at

    I do not have access to AX2012 right now, so could not try your code.

    But can't you close the fileStream in someway?

    Try one of these perhaps?

    fileStream.close();

    fileStream.finalize();

    fileStream = null;

  • Community Member Profile Picture
    on at

    i tried with fileStream = null but didn't work. let me use close() method.

  • Suggested answer
    Rudi Hansen Profile Picture
    4,075 on at

    Ok did that solve the problem?

  • Community Member Profile Picture
    on at

    Hi Rudi,

    Close method worked. Thanks a lot.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans