C# 单文件FTP下载

标签 c# ftp download networkcredentials

我正在尝试在 C# 控制台应用程序中使用 FTP 下载文件,但即使我现在路径正确,我总是收到错误消息“550 文件未找到”。

有什么方法可以返回当前路径(一旦连接到服务器)?

// 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!");
        }

编辑 我的错。修复了文件路径,仍然出现相同的错误。但如果我与 FileZilla 连接,这就是确切的文件路径。

最佳答案

最终通过使用 System.Net.FtpClient ( https://netftp.codeplex.com/releases/view/95632 ) 并使用以下代码找到了解决方案。

// 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);
                }
            }
        }

关于C# 单文件FTP下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28132628/

相关文章:

java - JSch SFTP 详细日志记录

ios - 在 YouTube 上使用 get_video 下载视频

android - 创建一个图像库,通过奇怪的滑动操作从网络缓存和下载图像

c# - 如何强制 Entity Framework 映射 `internal` 导航属性?

c# - 将数据保存到 DocumentDb 中的多个集合中

c# - 类型或 namespace 定义,或预期的文件结尾

java - 避免使用 Java FTP 下载以前下载的文件

ubuntu - 具有组写访问权限的用户的 SFTP 覆盖失败

c# - 无法在 FormView 的 <EditItemTemplate> 中显示内容

flash - 如何下载使用 SWFObject 嵌入的 Flash 文件