Search code examples
axaptafile-handlingdynamics-ax-2012

Move a file, AX2012


I'm trying to move a file, nothing clever.

The problem I am having is explained in the AX WONDERS blog.

The reason for this is that when using an AX class that runs on the server, the exception never comes back to the client and therefore cannot be handled correctly.... the operation will not fall into the Exception::CRLError exception

If the source file is opened by MSWord, for example, an exception is thrown in the fileLocked method, which is both infuriating yet amusing.

Any suggestions most welcome!

Some code:

server static void moveFile(str fileName, str newFileName)
{
    #File
    Set                 permissionSet;
    ;

    permissionSet =  new Set(Types::Class);
    //permissionSet.add(new FileIOPermission(fileName,#io_write));
    permissionSet.add(new FileIOPermission('',#io_write));
    permissionSet.add(new InteropPermission(InteropKind::ClrInterop));

    CodeAccessPermission::assertMultiple(permissionSet);

    if (isRunningOnServer()) 
    { 
        if (WinAPIServer::fileExists(newFileName))
            WinAPIServer::deleteFile(newFileName);
        WinAPIServer::copyFile(fileName, newFileName);
        if (!WinAPIServer::fileLocked(fileName))
            WinAPIServer::deleteFile(fileName);
    }
    else
    {
        if (WinApi::fileExists(newFileName))
            WinApi::deleteFile(newFileName);
        WinAPI::copyFile(fileName, newFileName);
        if (!WinAPI::fileLocked(fileName))
            WinAPI::deleteFile(fileName);
    }
    //System.IO.File::Move(fileName, newFileName);

    CodeAccessPermission::revertAssert();
}

Error registry:

System.IO.IOException: The process cannot access the file 'M:\Interfaces\Prod\ImportacionClientes\Direcciones\XXXXXXAD_20130711_1136.TXT' because it is being used by another process.

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

   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)

   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)

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

   at Dynamics.Ax.Application.WinAPIServer.fileLocked(String _fileName) in WinAPIServer.fileLocked.xpp:line 33

   at Dynamics.Ax.Application.EVE_UlaboxInterfaceClientes_IN.moveFile(String fileName, String newFileName) in EVE_UlaboxInterfaceClientes_IN.moveFile.xpp:line 19

Solution

  • I would go for the

     System.IO.File::Move(fileName, newFileName);
    

    instead of being to clever. Consider the prior existence of the newFileName an error.

    Don't use the WinAPI file methods in a server or batch context (you checked that).
    Using both WinAPI and WinAPIServer is just too painful, go straight for the .Net methods.

    Most likely the fileLocked is in error.