Writing a FTP automation

Over some time I have been using a plain dos ftp from Navision. There is some advantages and some disadvantage combined with it.

A advantage is, that this use does not require any third part products; but a disadvantage is that you does not have the fully control over the file transfer, so you have to build your own kind of file log and parsing it to know if everything went well.

Therefor I decided to build my own automation for Navision in c#.

My first version of the automation should only contain some basic functions.
Which are:
– Upload File
– Download File
– List

To upload a file we will be using FTPWebRequest, which is a part of System.Net and FileInfo,
which is a part of System.IO.

We will start with making a connection to the FTP Server:

 cside |  copy code |? 
string ConnectionString = "ftp://" + strFTPServer + ':' +intFTPPort + "/" +strFtpDirectory +FileName;
FtpWebRequest FtpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ConnectionString));
FtpRequest.Credentials = new NetworkCredential(strUsername, strPassword);

If the connection went well, we are ready for to upload the file. The upload is done by
creating a File Stream and then writing the file byte by byte to the Ftp Server.

 cside |  copy code |? 
FtpRequest.Method = WebRequestMethods.Ftp.UploadFile;
FtpRequest.ContentLength = FileInf.Length;
FileInfo FileInf = new FileInfo(FileName);
int BufferLength = 2048;
byte[] Buffer = new byte[BufferLength];
int ContentLength;
FileStream UpStream = FileInf.OpenRead();
Stream FtpStream = FtpRequest.GetRequestStream();                   
ContentLength = UpStream.Read(Buffer, 0, BufferLength);
while (ContentLength != 0)
   FtpStream.Write(Buffer, 0, ContentLength);
   ContentLength = UpStream.Read(Buffer, 0, BufferLength);

To download a file is pretty similar to upload a file.

First we start with making a connection to the Server:

 cside |  copy code |? 
FtpWebRequest FtpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ConnectionString));
FtpRequest.Credentials = new NetworkCredential(strUsername, strPassword);

and then we downloads the file by using a stream:

 cside |  copy code |? 
System.IO.Stream ResponseStream = null;
System.IO.FileStream DownStream = null;
FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse WebResponse = (FtpWebResponse)FtpRequest.GetResponse();
ResponseStream = WebResponse.GetResponseStream();
DownStream = System.IO.File.Open(DownFileName + ".tmp", System.IO.FileMode.Create);
while (((iWork = ResponseStream.Read(buf, 0, buf.Length)) > 0))
   DownStream.Write(buf, 0, iWork);

If you want to check that the download went well – you can always check the return response
from the FtpRequest:

 cside |  copy code |? 
FtpWebResponse ReturnResponse = ((FtpWebResponse)FtpRequest.GetResponse());
if (StatusOk(ReturnResponse))
   DownloadSucceeded = true;

And then finally the List functionality. This function will be useful when downloading files.

Again we start with making a connection to the Server:

 cside |  copy code |? 
FtpWebRequest FtpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ConnectionString));
FtpRequest.Credentials = new NetworkCredential(strUsername, strPassword);

Then we use the ListDirectoryDetails funtion to find the files.

 cside |  copy code |? 
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse Response = (FtpWebResponse)FtpRequest.GetResponse();

As you can see, where are again using the stream functionality.

 cside |  copy code |? 
StringBuilder Result = new StringBuilder();
StreamReader Reader = new StreamReader(Response.GetResponseStream());
string Line = Reader.ReadLine();
string tmpLine;
while (Line != null)
   tmpLine = Line;
   //Lines starting with d is directories - everything else is file
   if (!tmpLine.StartsWith("d"))
      tmpLine = Line.Substring(Line.LastIndexOf(' ') + 1);
   Line = Reader.ReadLine();
string[] FileList
FileList = Result.ToString().Split('\n');

Thats all – now you can upload, download and list files.

You can leave a response, or trackback from your own site.

6 Responses to “Writing a FTP automation”

  1. Jens says:

    As allways quality NAVstuff for the NAVaddicted :-) Normally I would use SQL 2005 to operate the FTP-service, but I guess it’s just me playing the part of ‘for a Blacksmith every problem can be solved with a solid Sledgehammer’ :-)

  2. Bent says:

    And you can expose the directory list to Navision via the INavEnumerator :-)

    I wonder about streaming into Navision. It might be interesting to stream into a blob field – say you want to download an product image directly to a blob field in a product table.

    Do you remember if it’s possible to stream (or transfer) binaries directly into Navision from an automation?

    It’s quite a neat idea that it’s possible to have Navision be an FTP client.

  3. Nicklas says:

    Hi. Im searching for such a automation for my NAV project, is it possible you will send me the automations?

  4. iby says:

    Sorry – but I don’t send out the automations, because they are used at my Work.
    But if you use the code snippets, it should be possible for you to build your own automation from it :-)

  5. sanato says:

    Nice work! I’ll have to do a cross post on this one ;)

  6. raver2046 says:

    Can you put the compiled dll in your blog , thanks.

Leave a Reply

6 − four =