Download all files from a FTP Directory

Apr 29, 2013 at 2:08 PM
Hi,

I'm trying to download (move) all files in a specifique directory on a FTPServer to my local disk using C# code.
This is to get XML files from a transporter using their FTP.

So this is the start-situation:

FTPServer:
  • \OUT\
     - file 1.xml
     - file 2.xml
     - file 3.xml
    
Local disk:
  • \IN\
     - nothing
    
After my code has run:

FTPServer:
  • \OUT\
    - nothing
    
Local disk:
  • \IN\
     - file 1.xml
     - file 2.xml
     - file 3.xml
    
I can't find an example of how to do this with System.Net.FtpClient.
I could retrieve the listing of files and directory without any problems,
but don't know the code to download the files in the listing.

Thanks in advance!
Coordinator
Apr 29, 2013 at 2:10 PM
Check out the OpenRead example.
Apr 29, 2013 at 3:04 PM
jptrosclair wrote:
Check out the OpenRead example.
Would following example be a good implementation?
 public override bool GetNextExists(object request)
        {
            //Make FTP Connection, and download all files to local directory
            using (FtpClient ftp = new FtpClient())
            {
                try
                {
                    ftp.Host = FTPAddress;
                    ftp.Credentials = new NetworkCredential(FTPUser, FTPPassword);
                    ftp.Port = FTPPort;
                    ftp.DataConnectionType = FtpDataConnectionType.AutoPassive;

                    ftp.Connect();

                    if (ftp.IsConnected)
                    {
                        FtpListItem[] items = ftp.GetListing();
                        //here you can get list with type, name, modified date and other properties. 
                        foreach (var ftpListItem in items)
                        {
                            if (ftpListItem.Type == FtpFileSystemObjectType.File)
                            {
                                using (Stream ftpStream = ftp.OpenRead(ftpListItem.FullName))
                                {
                                    try
                                    {
                                        //This will actually save the file to local directory, we do nothing with the FileInfo object here, but might later on
                                        FileInfo file = Utils.Helpers.StreamHelper.SaveStreamToFile(LocalINFolder + ftpListItem.Name, ftpStream);
                                    }
                                    finally
                                    {
                                        Console.WriteLine();
                                        ftpStream.Close();
                                    }
                                }
                                //Remove File From FTP Directory
                                ftp.DeleteFile(ftpListItem.FullName);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Program.DBErrorLog(ex, RequestName);
                }
                finally
                {
                    ftp.Disconnect();
                }
            }

            //Check if there are any Existing xml files on local directory
            return XMLManager.NewFilesExists("", LocalINFolder);
        }
In Utils:
public static FileInfo SaveStreamToFile(string fileFullPath, Stream stream)
        {
            FileInfo newFile = null;

            if (stream.Length != 0)
            {
                // Create a FileStream object to write a stream to a file
                using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int) stream.Length))
                {
                    // Fill the bytes[] array with the stream data
                    var bytesInStream = new byte[stream.Length];
                    stream.Read(bytesInStream, 0, (int) bytesInStream.Length);

                    // Use FileStream object to write to the specified file
                    fileStream.Write(bytesInStream, 0, bytesInStream.Length);

                    newFile = new FileInfo(fileFullPath);
                }
            }

            return newFile;
        }
Apr 1, 2014 at 9:11 PM
swifty_programmer Could you please post the opposite of your code? I mean uploading ftp files from local folder with foreach loop.
I'm using your code successfully while downloading from ftp. But I can't upload multiple files at the same time.. Please need help.
Thanks in advance
Apr 2, 2014 at 7:47 AM
Hi,

I didn't need to upload files back to the ftp.
It is significantly more difficult, but very do-able.

Here's a link to help you:http://www.techrepublic.com/blog/how-do-i/how-do-i-use-c-to-upload-and-download-files-from-an-ftp-server/

But you can google it for other solutions too.

I hope I could help!
Oct 13, 2014 at 6:35 PM
May be too late to reply now, but may be helpful for others who search for the same scenario:
using System.Net.FtpClient;

public class FtpUpload
{
    public void UploadFileToFtp
    {
        using (var ftpClient = new FtpClient())
        {
            var destPath = "MyDirectory"
            var sourcePath = @"C:\Files to Upload\MyFile.zip";

            ftpClient.Host = "ftp.schiffhauer.com";
            ftpClient.Credentials = new NetworkCredential("chris", "letmein");

            ftpClient.Connect();

            using (var fileStream = File.OpenRead(sourcePath))
            using (var ftpStream = ftpClient.OpenWrite(string.Format("{0}/{1}", destPath, Path.GetFileName(sourcePath))))
            {
                var buffer = new byte[8 * 1024];
                int count;
                while ((count = fileStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ftpStream.Write(buffer, 0, count);
                }
            }
        }
    }
}
I have found that on here