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('ftp://10.1.10.67','username','password', '*.zip');

// For example this will retrieve all files
// ListFiles('ftp://10.1.10.67','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);
END;

StreamReader.Close;
Stream.Close;

IF GUIALLOWED THEN BEGIN
  MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription);
END;


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.


Microsoft Dynamics NAV: Download with FTP using dotNET Interop

Having an extensive C# development background, as I had mentioned in a previous post, I was thrilled with the ability to access the .NET framework through the dotNET Interoperability introduced with the Microsoft Dynamics NAV Roletailored client.

When interfacing data with other systems it is often necessary to upload files via FTP. It may also be necessary to download files via FTP.  This can be accomplished directly in Microsoft Dynamics NAV via Interop.



The local variables defined:
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'
SIOFile	DotNet	System.IO.File.'mscorlib'
StreamReader	DotNet	System.IO.StreamReader.'mscorlib'
MemoryStream	DotNet	System.IO.MemoryStream.'mscorlib'

The method to download the file from the remote site:

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

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

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

filename := path + filename;

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

// Text files only
//StreamReader := StreamReader.StreamReader(Stream);
//SIOFile.WriteAllText(path + filename, StreamReader.ReadToEnd);

MemoryStream := MemoryStream.MemoryStream();
Stream.CopyTo(MemoryStream);
SIOFile.WriteAllBytes(filename, MemoryStream.GetBuffer());

Stream.Close;

IF GUIALLOWED THEN BEGIN
  MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription);
END;

Microsoft Dynamics NAV: FTPWebRequest Methods String Values

I had previously posted on using dotNET Interop to FTP a file to a remote server. In the code the command is specified in the line: FtpWebRequest.Method := 'STOR';  The other methods available are:

'APPE'  AppendFile
'DELE'  DeleteFile
'RETR'  DownloadFile
'MDTM'  GetDateTimestamp
'SIZE'  GetFileSize
'NLIST' ListDirectory
'LIST'  ListDirectoryDetails
'MKD'   MakeDirectory
'PWD'   PrintWorkingDirectory
'RMD'   RemoveDirectory
'RENAME' Rename
'STOR'  UploadFile
'STOU'  UploadFileWithUniqueName 


Microsoft Dynamics NAV: Upload with FTP using dotNET Interop

One of the biggest additions to the development environment with the advent of the RoleTailored Client Microsoft and Dynamics NAV Server was the availability of the Microsoft .NET Framework assemblies. This allows Microsoft Dynamics NAV objects can interact with .NET Framework objects. You can easily use assemblies from the .NET Framework class library, your own custom assemblies; or third-party assemblies. This allows you to extend your solution and not be limited to only the NAV or COM objects.

One such example of using dotNET Interoperability is the ability to use the dotNET framework to FTP files to a remote server. Without access to the dotNET framework it was necessary to create a third party COM control, call a batch file, or schedule some third party software to transfer a file. Now, you can generate and send files all from within NAV. This can centralize the task of generating and transmitting files to one application.


A function that can be used (and easily extended) to upload a file to a remote FTP server via dotNET Interop in NAV is listed below.

Local Variables

 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'
SIOFile			DotNet		System.IO.File.'mscorlib'



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

FtpWebRequest := FtpWebRequest.Create(siteaddress + '/' + filename);

FtpWebRequest.Credentials := NetworkCredential.NetworkCredential(username, password);
FtpWebRequest.Method := 'STOR';
FtpWebRequest.KeepAlive := TRUE;
FtpWebRequest.UseBinary := TRUE;
//FtpWebRequest.UsePassive := FALSE;
filename := path + filename;

FtpWebRequest.ContentLength := SIOFile.ReadAllBytes(filename).Length;
Stream := FtpWebRequest.GetRequestStream;
Stream.Write(SIOFile.ReadAllBytes(filename), 0, FtpWebRequest.ContentLength); Stream.Close; FtpWebResponse := FtpWebRequest.GetResponse; IF GUIALLOWED THEN BEGIN MESSAGE('%1 %2', FtpWebResponse.StatusCode, FtpWebResponse.StatusDescription); END;

Mac: Expand all folders in finder list view mode



Have you ever spent the time clicking on each level in a Finder window to expand the tree below? Have you ever wish that there was a way on your Mac to expand all the subfolders below a folder? While there isn’t an option on the menu you can accomplish this by pressing and holding the Option button while you expand (click the triangle to the left of the folder) a folder. Pressing and holding the Option button as you click will expand all the folders below a folder while in list view mode.

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.