如何用C#连接FileZilla SFTP服务器并自动下载指定文件?
C#连接FileZilla SFTP服务器下载文件问题
我想用C#连接FileZilla SFTP服务器,自动将选定的文件下载到本地文件夹,但尝试以下代码后无法完成下载。我不确定是否需要用窗体应用实现连接,或是需要添加按钮触发连接操作,求技术帮助。
string Host = "sftp..."; string Username = "login"; string Password = "password"; using (SftpClient sftp = new SftpClient(Host, 88, Username, Password)) { try { sftp.Connect(); if (sftp.IsConnected) //连接成功进入这里... { label1.Text = "connection successful."; } else { label1.Text = "connection unsuccessful!"; } } catch { label1.Text = "error!"; } } //我写的从服务器下载文件的代码 string remotePath = "sftp://login**/0001.7z"; string localPath = "\"C:\\Users\\Halis\\Desktop\\indirdiğim\""; string host = "sftp..."; string username = "login name"; string password = "password"; // using (WebClient sftpClient = new WebClient()) { sftpClient.Credentials = new NetworkCredential(username, password); try { string sftpServer = "sftp..."; string remoteFilePath = "sftp:/login**//0001.7z"; string localFilePath = "\"C:\\Users\\Halis\\Desktop\\indirdiğim\""; sftpClient.DownloadFile($"{sftpServer}/{remoteFilePath}", localFilePath); Console.WriteLine("Dosya indirildi."); } catch (WebException ex) { Console.WriteLine($"Hata: {ex.Message}"); } }
问题分析与解决方案
1. 代码核心问题
- WebClient不支持SFTP:
WebClient原生仅支持HTTP/HTTPS/FTP协议,不兼容SFTP,这是下载失败的根本原因。必须统一使用SftpClient(来自SSH.NET库)完成连接和下载操作。 - 路径格式错误:
- 远程路径无需
sftp://前缀,直接写服务器上的相对或绝对路径即可,比如/0001.7z(若文件在用户根目录,直接写0001.7z也可)。 - 本地路径不要加多余引号,直接写
C:\\Users\\Halis\\Desktop\\indirdiğim,且要确保目标文件夹已存在,否则会抛出异常。
- 远程路径无需
- 重复连接浪费资源:代码中初始化了两次SFTP相关对象,应复用同一个
SftpClient连接完成所有操作。
2. 关于窗体应用与触发方式
- 不是必须用窗体应用:控制台应用也能实现自动下载,运行程序即可执行逻辑;如果需要手动触发,窗体加按钮是常见方式,但核心逻辑和控制台一致。
- 若用窗体应用,建议将SFTP操作放在异步方法中,避免阻塞UI线程导致界面假死。
修正后的完整代码
控制台版本(自动执行下载)
using Renci.SshNet; using System; class Program { static void Main() { string host = "你的SFTP服务器地址"; int port = 88; // 注意:SFTP默认端口是22,需和你的FileZilla服务器配置一致 string username = "你的用户名"; string password = "你的密码"; string remoteFilePath = "/0001.7z"; // 服务器上的文件路径 string localFolderPath = @"C:\Users\Halis\Desktop\indirdiğim"; string localFilePath = System.IO.Path.Combine(localFolderPath, "0001.7z"); // 确保本地文件夹存在 if (!System.IO.Directory.Exists(localFolderPath)) { System.IO.Directory.CreateDirectory(localFolderPath); } using (SftpClient sftp = new SftpClient(host, port, username, password)) { try { sftp.Connect(); Console.WriteLine("连接成功"); // 下载文件 using (var localFileStream = System.IO.File.Create(localFilePath)) { sftp.DownloadFile(remoteFilePath, localFileStream); } Console.WriteLine("文件下载完成"); sftp.Disconnect(); } catch (Exception ex) { Console.WriteLine($"错误:{ex.Message}"); } } } }
Windows窗体版本(按钮触发)
在窗体中添加按钮btnDownload和标签labelStatus,代码如下:
using Renci.SshNet; using System; using System.IO; using System.Threading.Tasks; using System.Windows.Forms; public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private async void btnDownload_Click(object sender, EventArgs e) { labelStatus.Text = "正在连接..."; string host = "你的SFTP服务器地址"; int port = 88; string username = "你的用户名"; string password = "你的密码"; string remoteFilePath = "/0001.7z"; string localFolderPath = @"C:\Users\Halis\Desktop\indirdiğim"; string localFilePath = Path.Combine(localFolderPath, "0001.7z"); try { // 确保本地文件夹存在 if (!Directory.Exists(localFolderPath)) { Directory.CreateDirectory(localFolderPath); } using (SftpClient sftp = new SftpClient(host, port, username, password)) { await Task.Run(() => { sftp.Connect(); using (var localFileStream = File.Create(localFilePath)) { sftp.DownloadFile(remoteFilePath, localFileStream); } sftp.Disconnect(); }); labelStatus.Text = "文件下载完成"; } } catch (Exception ex) { labelStatus.Text = $"错误:{ex.Message}"; } } }
注意事项
- 先安装
SSH.NETNuGet包:右键项目 → 管理NuGet程序包 → 搜索SSH.NET并安装。 - 确认FileZilla SFTP服务器的端口配置(默认是22,代码中用了88,需和服务器一致)。
- 检查服务器文件路径是否正确,以及用户是否拥有该文件的读取权限。
内容的提问来源于stack exchange,提问作者Halis Execute
相关产品推荐
相关产品推荐

