Interview Questions

Monday, May 12, 2014

Move File from One Folder to Another Folder using X++ ax 2009

Today i’m going to share you that , how to move a File from One Folder to another folder using X++.
Here we can achieve this with the Help of WINAPI(Windows Application Programming Interface). Even though we can do Copy a file, delete a file, Create Directory, getting the file Size, etc. Refer more Details WINAPI

static void MoveFilesFromFoldertoAnotherFolder(Args _args)
{
FilenameOpen fileNameOpen;
DialogField dialogFileName;
Dialog dialog;
Filename filePath;
Filename fileName;
Filename fileType;
FileName DestinationPath;
#File

;
DestinationPath = @'C:\Users\saadullah\Desktop\Dest\'; // Define your Destination Path
dialog = new Dialog("Move Files");
// AX 2009
dialogFilename = dialog.addField(typeId(FileNameOpen));
// AX 2012
// dialogFileName = dialog.addField(extendedTypeStr(FileNameOpen));
dialog.filenameLookupFilter([#AllFilesType]);
dialog.filenameLookupTitle("Select File");
dialog.caption("Move File");
dialogFilename.value(fileName);
if(!dialog.run())
return;
filenameOpen = dialogFilename.value();
[filePath, fileName, fileType] = fileNameSplit(fileNameOpen);
// MoveFile - The Original File won't be available once it's moved into destination path
WinAPI::moveFile(fileNameOpen, DestinationPath+FileName+FileType);
// CopyFile - The Original File will be available even if it's moved into destination path
//WinAPI::copyFile(fileNameOpen, DestinationPath+FileName+FileType);
// DeleteFile - Delete the Selected File
//WinAPI::deleteFile(fileNameOpen);

// If u need to Use this in Form Take look in AOT > Forms > Tutorial_Form_File
}

static void moveFile(args _args)
{
    #File
    Set                 permissionSet;
    System.Exception    netExcepn;
    str FileName = 'Libraries\Pictures\Images.jpg';
    str NewFileName = 'C:\Images.jpg';
    ;
    try
    {
        permissionSet =  new Set(Types::Class);
        permissionSet.add(new FileIOPermission(fileName,#io_write));
        permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
        CodeAccessPermission::assertMultiple(permissionSet);
        WINAPI::copyFile(fileName,newFileName,true);
        CodeAccessPermission::revertAssert();
    }
    catch (Exception::CLRError)
    {
        info("Caught 'Exception::CLRError'.");
        netExcepn = CLRInterop::getLastException();
        info(netExcepn.ToString());
    }
}

No comments:

Post a Comment