[dba-VB] FYI: Programming ftp in .NET

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Mon May 5 08:03:23 CDT 2008


Hi All,

It happens that basic FTP access - login, change dir, upload and download
can be relatively easy programmed by using this free code -
http://www.ondotnet.com/pub/a/dotnet/2004/05/10/ftpdotnet.htm ...

I have found just one issue: while uploading a file having left or right
curly bracket - '{' or '}' in ASCII mode I'm getting "Input line wasn't in
correct format" runtime error on this code line:

public class FTPClient
...
private void PutASCII(Stream srcStream, string remoteFile, bool append)
...
     writer.Write(line, 0, line.Length);
...

When I make curly bracket duplicated then it works well, but then download
doesn't work again breaking on curly bracket...

Have you seen issues like that? (I guess I have here FTP protocol specs
violation in the case of curly brackets?)

In BINARY mode everything seems to work well, therefore issue isn't that
critical still I'd be interested to know what other chars could result in
runtime errors? And how to properly escape curly brackets to implement FTP
put and get in ASCII mode....

BTW, in P.S. you can find simple C# Windows Console app FTP test of the
referred above free source

Thanks.

--
Shamil

P.S.


using System;
using System.Collections.Generic;
using System.Text;

using com.enterprisedt.net.ftp;
using FTP;

namespace FTPTestConsole
{
    class Program
    {
        private static void logger_OnLoggableEvent(object o,
LogInfoEventArgs e)
        {
            //lbProgress.Items.Add(e.Message);
            //lbProgress.SelectedIndex = lbProgress.Items.Count - 1;
            //Application.DoEvents();
            Console.WriteLine(e.Message);   
        }

        private static string _host = "{{yourHostHere}}";
        private static string _userId = "{{yourUserNameHere}}";
        private static string _password = "{{yourPasswordHere}}";

        private static string _ftpFolder = "testUp";

        private static string _localUploadedFileFullPath =
@"E:\temp\up.txt";
        private static string _localDownloadedFileFullPath =
@"E:\temp\OK.txt";
        private static string _remoteFileName = "test.txt";

        static void Main(string[] args)
        {
            try
            {
                // Create and register a logger
                FTP.Logger logger = new FTP.Logger();
                //logger.OnLoggableEvent += new
FTP.Logger.LoggableEventHandler(logger_OnLoggableEvent);
                logger.OnLoggableEvent += new
Logger.LoggableEventHandler(logger_OnLoggableEvent);

                // create an instance of FTPClient, passing in the name of
hte host, the time out
                // and the logger for updates

                string host = _host;
                com.enterprisedt.net.ftp.FTPClient ftpClient = new
com.enterprisedt.net.ftp.FTPClient(host);

                // call login, passing in the user id and the password
                string user = _userId;
                string pw = _password;
                ftpClient.Login(user, pw);

                // change directory on the host
                logger.Write(null, "Client requesting change directory to "
+ _ftpFolder);
                string newDirectory = _ftpFolder;
                ftpClient.Chdir(newDirectory);

                // send a file to the host (rename it there)
                logger.Write(null, "Client calling Put(" +
_localUploadedFileFullPath + "," + _remoteFileName + ")");
                string localFileName = _localUploadedFileFullPath;
                string remoteFileName = _remoteFileName;
                //ftpClient.Put(localFileName, remoteFileName);
                ftpClient.TransferType = FTPTransferType.BINARY;  
                ftpClient.Put(localFileName, remoteFileName);

                // ge the file back from the host, rename it here
                logger.Write(null, "Client calling Get(" +
_localDownloadedFileFullPath + "," + _remoteFileName + ")");
                string newFileName = _localDownloadedFileFullPath;
                ftpClient.Get(newFileName, remoteFileName);

                // clean up and go home
                logger.Write(null, "Client calling quit");
                ftpClient.Quit();
                logger.Write(null, "Done.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);   
            }
        }
    }
}




More information about the dba-VB mailing list