我試圖移動一個文件,沒有什麼聰明的。移動文件,AX2012
我遇到的問題在AX WONDERS blog中有解釋。
這樣做的原因是,使用該服務器上運行的AX類時,除了永不再來給客戶,因此不能正確處理....操作不會陷入異常: :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 :: Delete(newFileName);'第一個。不要忘記趕上'CLRError'。 –