簡體   English   中英

移動文件,AX2012

[英]Move a file, AX2012

我正在嘗試移動文件,沒有什么聰明的。

AX WONDERS博客中解釋了我遇到的問題。

原因是當使用在服務器上運行的AX類時,該異常永遠不會返回到客戶端,因此無法正確處理。...該操作不會屬於Exception :: CRLError異常

例如,如果源文件由MSWord打開,則fileLocked方法中將引發異常,這既令人發怒又很有趣。

任何建議最歡迎!

一些代碼:

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();
}

錯誤注冊表:

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

我會去

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

而不是聰明。 newFileName的先前存在newFileName錯誤。

不要在服務器或批處理上下文中使用WinAPI文件方法(已檢查)。
同時使用WinAPIWinAPIServer太痛苦了,直接使用.Net方法。

很可能是fileLocked錯誤。

您正在嘗試訪問newFileName而不聲明其權限。 此外,在批處理過程中, isRuningOnServer()方法在所有情況下均無法正常工作,因此我將研究它是否在您的情況下可以正常工作。

在批量使用TextIo及其基類時,即使您將對象分配為null也不會釋放文件讀取鎖定。

使用StreamReader重寫導入,並使用.close()和.dispose()它將起作用。

編輯:在TextIo上調用finalize()也會關閉文件,也可以批量運行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM