C#单文件FTP下载

编程入门 行业动态 更新时间:2024-10-24 12:25:21
本文介绍了C#单文件FTP下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正试图在C#控制台应用程序中下载FTP文件,但即使我现在的路径是正确的,我总是收到一个错误,说明550文件未找到。

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

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

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

推荐答案

最后通过使用System.Net.FtpClient( netftp.codeplex/releases/view/95632 ),并使用以下内容代码

Finally found a solution by using System.Net.FtpClient (netftp.codeplex/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); } } }

更多推荐

C#单文件FTP下载

本文发布于:2023-11-07 22:40:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单文   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!