本文介紹了如何使用 C# 將多個(gè)文件從 FTP 服務(wù)器傳輸?shù)奖镜啬夸?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號(hào)..
我可以將一個(gè)文件從 ftp 服務(wù)器傳輸?shù)奖镜啬夸?使用以下代碼
I can transfer one file from ftp server to local directory. using the following code
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential("username", "password");
ftpClient.DownloadFile("ftp://website.com/abcd.docx", @"D:\WestHam est.docx");
但我不知道如何傳輸多個(gè)文件.任何人都可以幫我解決這個(gè)問題.}
but i don't know how to transfer multiple files. can anybody help me on this. }
推薦答案
使用此代碼,只需替換用戶憑據(jù):
Use this code, just replace the user credentials:
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);
}
}
}
這篇關(guān)于如何使用 C# 將多個(gè)文件從 FTP 服務(wù)器傳輸?shù)奖镜啬夸?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!