Executing external programs

Sometimes it’s needed to add functionality not delivered by NAV. One way is to use .Net assemblies or automations. Another scenario can be to run an external program in C/AL. One way is to use the quite old SHELL Command or class WshShell of the “Windows Scripting Host” (automation). In both cases, when running an external program, a security warning is displayed … not so good for automatic execution (batch).

Class System.Diagnostics.Process from .Net assembly System.dll
To avoid that and to have also a much prettier solution, use the class System.Diagnostics.Process from the .Net Framework (search for assembly System.dll). For usage in RTC Client set the property RunOnClient to true, for usage on the server only like job queues set the value to false.

Sample: creating a file using the dir command in a dos shell

// variable process | dotnet | System.Diagnostics.Process (assembly System.dll)
process := process.Process;
process.StartInfo.UseShellExecute := FALSE;
process.StartInfo.FileName := ‘cmd.exe’;
process.StartInfo.Arguments := ‘/c dir > c:\temp\dir.txt’;
process.StartInfo.CreateNoWindow := TRUE;
process.Start();
CLEAR(process);

sample: extract a rar file

process := process.Process;
process.StartInfo.UseShellExecute := FALSE;
process.StartInfo.FileName := ‘”C:\Program Files (x86)\WinRAR\unrar.exe”‘;
process.StartInfo.Arguments := ‘e -y -o+ c:\temp\archive.rar c:\temp’;
process.StartInfo.CreateNoWindow := TRUE;
process.Start();
CLEAR(process);

Stop pending processes:
Use methods WaitForExit or Kill to stop pending processes.

Links:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process%28v=vs.110%29.aspx
http://msdn.microsoft.com/en-us/library/dd355282.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit(v=vs.110).aspx

cheers

Advertisements

2 thoughts on “Executing external programs

  1. Hi Jonathan,

    I wanted to recreate the code in my NAV installation but I can’t find the System.Diagnostics.Process .NET assembly. The assemblies that I see are System.Diagnostics.Contracts/Debug/Tools/Tracing. Am I missing something?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s