我正在尝试移动文件,没有什么聪明的。
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