gpt4 book ai didi

c# - 如何使用 C# 将多个文件从 FTP 服务器传输到本地目录?

转载 作者:太空狗 更新时间:2023-10-29 22:13:53 26 4
gpt4 key购买 nike

我可以将一个文件从 ftp 服务器传输到本地目录。使用以下代码

  using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential("username", "password");
ftpClient.DownloadFile("ftp://website.com/abcd.docx", @"D:\\WestHam\test.docx");

但我不知道如何传输多个文件。谁能帮我解决这个问题。

最佳答案

使用此代码,只需替换用户凭据:

static void Main(string[] args)
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://mywebsite.com/");
ftpRequest.Credentials = new NetworkCredential("user345", "pass234");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();

string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();


using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential("user345", "pass234");

for (int i = 0; i <= directories.Count-1; i++)
{
if (directories[i].Contains("."))
{

string path = "ftp://mywebsite.com/" + directories[i].ToString();
string trnsfrpth = @"D:\\Test\" + directories[i].ToString();
ftpClient.DownloadFile(path, trnsfrpth);
}
}
}

关于c# - 如何使用 C# 将多个文件从 FTP 服务器传输到本地目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com