-->

C# single file FTP download

2019-05-20 11:04发布

问题:

I am trying to download a file usng FTP within a C# console application, but even though I now the paths are correct I always get an error saying "550 file not found".

Is there any way, to return the current path (once connected to the server)?

// lade datei von FTP server
        string ftpfullpath = "ftp://" + Properties.Settings.Default.FTP_Server + Properties.Settings.Default.FTP_Pfad + "/" + Properties.Settings.Default.FTP_Dateiname;
        Console.WriteLine("Starte Download von: " + ftpfullpath);
        using (WebClient request = new WebClient())
        {
            request.Credentials = new NetworkCredential(Properties.Settings.Default.FTP_User, Properties.Settings.Default.FTP_Passwort);
            byte[] fileData = request.DownloadData(ftpfullpath);

            using (FileStream file = File.Create(@path + "/tmp/" + Properties.Settings.Default.FTP_Dateiname))
            {
                file.Write(fileData, 0, fileData.Length);
                file.Close();
            }
            Console.WriteLine("Download abgeschlossen!");
        }

EDIT My mistake. Fixed the filepath, still getting the same error. But if I connect with FileZilla that's the exact file path.

回答1:

Finally found a solution by using System.Net.FtpClient (https://netftp.codeplex.com/releases/view/95632) and using the following code.

// aktueller pfad
        string apppath = Directory.GetCurrentDirectory();

        Console.WriteLine("Bereite Download von FTP Server vor!");

        using (var ftpClient = new FtpClient())
        {
            ftpClient.Host = Properties.Settings.Default.FTP_Server;
            ftpClient.Credentials = new NetworkCredential(Properties.Settings.Default.FTP_User, Properties.Settings.Default.FTP_Passwort);
            var destinationDirectory = apppath + "\\Input";

            ftpClient.Connect();

            var destinationPath = string.Format(@"{0}\{1}", destinationDirectory, Properties.Settings.Default.FTP_Dateiname);
            Console.WriteLine("Starte Download von " + Properties.Settings.Default.FTP_Dateiname + " nach " + destinationPath);
            using (var ftpStream = ftpClient.OpenRead(Properties.Settings.Default.FTP_Pfad + "/" + Properties.Settings.Default.FTP_Dateiname))
            using (var fileStream = File.Create(destinationPath , (int)ftpStream.Length))
            {
                var buffer = new byte[8 * 1024];
                int count;
                while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    fileStream.Write(buffer, 0, count);
                }
            }
        }


回答2:

I think your filename is wrong. Your first line writes a different name than what you set to ftpfullpath. You us FTP_Dateiname on the first line but FTP_Pfad when you set ftpfullpath.

To see what's actually happening move your first line after 'string ftpfullpath...')

and change it to Console.WriteLine("Starte Download von: " + ftpfullpath);