简体   繁体   English

移动文件,AX2012

[英]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 . AX WONDERS博客中解释了我遇到的问题。

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 原因是当使用在服务器上运行的AX类时,该异常永远不会返回到客户端,因此无法正确处理。...该操作不会属于Exception :: CRLError异常

If the source file is opened by MSWord, for example, an exception is thrown in the fileLocked method, which is both infuriating yet amusing. 例如,如果源文件由MSWord打开,则fileLocked方法中将引发异常,这既令人发怒又很有趣。

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

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. newFileName的先前存在newFileName错误。

Don't use the WinAPI file methods in a server or batch context (you checked that). 不要在服务器或批处理上下文中使用WinAPI文件方法(已检查)。
Using both WinAPI and WinAPIServer is just too painful, go straight for the .Net methods. 同时使用WinAPIWinAPIServer太痛苦了,直接使用.Net方法。

Most likely the fileLocked is in error. 很可能是fileLocked错误。

You're trying to access the newFileName without asserting permissions on it. 您正在尝试访问newFileName而不声明其权限。 Moreover, the isRuningOnServer() method doesn't work as expected in all cases during batch processing, so I would investigate if it's working fine in your case. 此外,在批处理过程中, isRuningOnServer()方法在所有情况下均无法正常工作,因此我将研究它是否在您的情况下可以正常工作。

TextIo and the it's base classes don't release the file read lock when used in batch even if you assign the object null. 在批量使用TextIo及其基类时,即使您将对象分配为null也不会释放文件读取锁定。

Rewrite the import with StreamReader and use .close() and .dispose() and it will work. 使用StreamReader重写导入,并使用.close()和.dispose()它将起作用。

Edit: Calling finalize() on TextIo will close the file, also when running in batch. 编辑:在TextIo上调用finalize()也会关闭文件,也可以批量运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM