private void Button1_Click(object sender, EventArgs e)
{
string Server = "服务器URL";
string username = "账户";
string password = "密码";
string remote = "服务器路径";
string local = "D:\\Debug"; // 本地文件夹路径
// 创建本地文件夹(如果不存在)
if (!Directory.Exists(local))
{
Directory.CreateDirectory(local);
}
try
{
// 连接到 FTP 服务器
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new NetworkCredential(username, password);
// 获取 FTP 目录中的文件列表
string[] fileList = ftpClient.DownloadDirectoryList(remote);
// 下载并保存每个文件
foreach (string file in fileList)
{
string remoteFilePath= remote + "/" + file;
string localFilePath =
Path.Combine(local, file);
// 下载文件
ftpClient.DownloadFile
(remoteFilePath, localFilePath);
// 释放资源
ftpClient.Dispose();
}
MessageBox.Show("下载完成!");
}
}
catch (Exception ex)
{
MessageBox.Show("出现错误:" + ex.Message);
}
}
C# Winfrom中 下载PDF文件
最新推荐文章于 2024-03-19 10:44:34 发布