Monday 15 June 2020

AX 2012 Open file in specific application


Currently there is requirement to open the file in specific (Adobe reader) application from client. This can be done through the system.Diagnostics.process library.

For this, application you wish to open must be installed. You can get the exe file name (acroRd32.exe) from the install folder of the application.( usually c:\ program files)

Below code snippet will opening file in specific application and it not found then one with default application.

client static void PrintDocuments(Filename FileNameView)
{
    FileIoPermission    fileIoPermission;

    System.Diagnostics.Process process;
    System.Diagnostics.ProcessStartInfo processStartInfo;
    Filename                FileName;
    str                     tempPath;
    fileIoPermission    =   new FileIoPermission(FileNameView, 'rw');
    fileIoPermission.assert();
    try
    {
        if (System.IO.File::Exists(FileNameView))
        {
          
// this will open file in adobe reader application
            process = new System.Diagnostics.Process();
            processStartInfo = new System.Diagnostics.ProcessStartInfo();
            processStartInfo.set_FileName("acroRd32.exe");
            processStartInfo.set_Arguments(FileNameView);
            process.set_StartInfo(processStartInfo);
            process.Start();
            sleep(1000);

        }
    }
    catch
    {
//this will open file in default application
        WinAPI::shellExecute(FileNameView);
    }
}