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:
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.
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);
}FtpStream.Close();
FtpStream.Dispose();UpStream.Close();
UpStream.Dispose();
To download a file is pretty similar to upload a file.
First we start with making a connection to the Server:
FtpWebRequest FtpRequest =
……(FtpWebRequest)FtpWebRequest.Create(new Uri(ConnectionString));FtpRequest.Credentials = new NetworkCredential(strUsername, strPassword);
and then we downloads the file by using a stream:
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);ResponseStream.Close();
ResponseStream.Dispose();DownStream.Flush();
If you want to check that the download went well – you can always check the return response
from the FtpRequest:
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:
FtpWebRequest FtpRequest =
……(FtpWebRequest)FtpWebRequest.Create(new Uri(ConnectionString));FtpRequest.Credentials = new NetworkCredential(strUsername, strPassword);
Then we use the ListDirectoryDetails funtion to find the files.
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse Response = (FtpWebResponse)FtpRequest.GetResponse();
As you can see, where are again using the stream functionality.
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);
………..Result.Append(tmpLine);
………..Result.Append(“\n”);
……}
……Line = Reader.ReadLine();
}Reader.Close();
Response.Close();string[] FileList
FileList = Result.ToString().Split(‘\n’);
Thats all – now you can upload, download and list files.