【问题标题】:C# single file FTP downloadC#单文件FTP下载
【发布时间】:2015-03-23 20:00:11
【问题描述】:

我正在尝试在 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,那就是确切的文件路径。

【问题讨论】:

    标签: c# ftp download networkcredentials


    【解决方案1】:

    通过使用 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);
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      我认为你的文件名是错误的。您的第一行写入的名称与您设置为 ftpfullpath 的名称不同。您在第一行使用 FTP_Dateiname,但在设置 ftpfullpath 时使用 FTP_Pfad。

      要查看实际发生的情况,请将第一行移到 'string ftpfullpath...' 之后)

      并将其更改为 Console.WriteLine("Starte Download von: " + ftpfullpath);

      【讨论】:

      • 已修复。往上看。还是一样的错误。感谢您指出错误,现在尝试了几个小时,我在网上找到了不同的解决方案,但总是得到相同的结果......
      • 我假设您在 FTP_Server 之后没有遗漏 /? (我假设 FTP_Pfad 是一个目录?)
      • 另一种可能是您使用了不同的用户/密码,并且在 ftp 服务器上获得了不同的根目录。
      • 首先。感谢您抽出宝贵时间蒂姆。 FTP 路径类似于ftp.server.com/directory/file.zip,凭据与我在 FileZilla 中使用的相同
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 2010-10-29
      相关资源
      最近更新 更多