Microsoft Dynamics NAV: List FTP Files using dotNET Interop

This post continues the demonstration of extending the .NET Framework in Microsoft Dynamics NAV to transfer files via FTP.  We have already reviewed Uploading Files and Downloading Files via FTP from within Microsoft Dynamics NAV.   Another important part of exchanging files via FTP is the listing of files on the remote server.   The following code can be used to list the files on an FTP server.

The local variables are declared as:

Name	DataType	Subtype	
FtpWebRequest	DotNet	System.Net.FtpWebRequest.'System'	
FtpWebResponse	DotNet	System.Net.FtpWebResponse.'System'	
NetworkCredential	DotNet	System.Net.NetworkCredential.'System'	
Stream	DotNet	System.IO.Stream.'mscorlib'	
StreamReader	DotNet	System.IO.StreamReader.'mscorlib'

The function is declared as:

ListFiles(siteaddress : Text[250];username : Text[80];password : Text[80];filename : Text[250])

// If filename is not blank it will filter on the list
// For example this will retrieve all zip files
// ListFiles('','username','password', '*.zip');

// For example this will retrieve all files
// ListFiles('','username','password', '');

FtpWebRequest := FtpWebRequest.Create(siteaddress + '/' + filename);
FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(username, password);

FtpWebRequest.Method := 'LIST';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
//FtpWebRequest.UsePassive := FALSE;

FtpWebResponse := FtpWebRequest.GetResponse;
Stream := FtpWebResponse.GetResponseStream;

StreamReader := StreamReader.StreamReader(Stream);

WHILE NOT (StreamReader.EndOfStream) DO BEGIN
  MESSAGE('%1', StreamReader.ReadLine);


  MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription);

The data retrieved for each file in the code above will need to be further parsed to get the filename.

Having reviewed how to Upload a file with FTP, Download a file with FTP and Listing Files with FTP you can create something to perform  FTP client from within Dynamics NAV.

Do not forget to reference the FTPWebRequest Method string values.

Pingbacks and trackbacks (1)+

Add comment

Reading Stuff

Information in this document subject to change without notice.
All Software source code published is for demonstration and knowledge sharing purposes only. The Code is supplied "as is" without warranty as to result, performance or merchantability. Use at your own risk.
The opinions expressed herein are the opinions of the author and do not reflect those of any other entity.