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)

Access to file using System.IO

(0) ShareShare
ReportReport
Posted on by

I am reading in a csv file then moving it to folder once read.

I started with the WINApi but when I moved the process to runasbatch I got the usual error.

Now I have this code :-

    container fileList;

    System.Array dirList;

    Filename fileName;


    Filename        newFileName;

    Filename        baseFileName;

    filePath        path;

    Filename        extension;

    int ix;



    new InteropPermission(InteropKind::ClrInterop).assert();

    dirList = System.IO.Directory::GetFiles( #fromFolder,"*.csv" );

    for( ix=0; ix < ClrInterop::getAnyTypeForObject( dirList.get_Length() ); ix++ )

    {

      Filename = ClrInterop::getAnyTypeForObject( dirList.GetValue(ix));

      [path, baseFileName, extension] = Global::fileNameSplit(fileName);


      if(this.importFile(Filename))

      {

         newFileName = #processedFolder+"\\"+baseFileName+"_"+strrem(date2str(today(), 321,2,2,2,2,4,2),".")+"_"+strreplace(time2str(timenow(),3,3)," ","")+extension;

      }

      else

      {

          newFileName = #ErrorFolder+"\\"+baseFileName+"_"+strrem(date2str(today(), 321,2,2,2,2,4,2),".")+"_"+strreplace(time2str(timenow(),3,3)," ","")+extension;

      }

      //clear CommaIO from "ImportFile()"

      importFile = null;

      System.IO.File::Copy(Filename, newFileName, true);

      new InteropPermission(InteropKind::ClrInterop).assert();

      System.IO.File::Delete(filename);


    }

Which works great when I run it directly, however when I run it on the batch it gives me an error (detailed at the end of this post)

System.IO.IOException: The process cannot access the file '\\myserver\files\export.csv' because it is being used by another process.

Which is strange because the folder permissions are set to "everyone" full control, and when I run the process off the AOT its find and works.

Any ideas? 

 

Full error :-

System.IO.IOException: The process cannot access the file '\\abvsql001\files\export.csv' because it is being used by another process.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.File.Delete(String path)

   at Dynamics.Ax.Application.ImportGPinventTrans.Findandprocess() in ImportGPinventTrans.FindAndProcess.xpp:line 33

   at Dynamics.Ax.Application.ImportGPinventTrans.Run() in ImportGPinventTrans.run.xpp:line 3

   at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 50

   at BatchRun::runJobStatic(Object[] )

   at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)

   at BatchIL.taskThreadEntry(Object threadArg)

 

 

 

 

*This post is locked for comments

I have the same question (0)
  • Tony Hemy Profile Picture
    on at

    To clarify the "Move" is working as I get a new file as expected. The old file is not deleted however.

  • Nasheet Siddiqui Profile Picture
    440 on at

    This error is coming because you are using file in the loop and asking again for permission.

    Just delete this permission assign code before delete because you are already assign it before

    new InteropPermission(InteropKind::ClrInterop).assert();

    System.IO.File::Delete(filename);

  • Tony Hemy Profile Picture
    on at

    Hey Nasheet and thanks for the reply.

    I added the extra assert in just to see if that would resolve the issue but it does not. I get the same error with it there or with out it there.

  • Tony Hemy Profile Picture
    on at

    Any other suggestions?

  • Suggested answer
    Andrew Jones a1x Profile Picture
    136 on at

    Old thread, but I just had the same issue and couldn't find any answer by Googling the problem. I actually solved it by calling finalize() on the commaIO object instead of setting it to null.

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
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans