下载文件的列表,从ftp到本地文件夹中使用C#?
我期待在FTP中的所有文件下载到本地folder.All的文件应该在FTP被删除,一旦下载到本地驱动器。
I am looking to download all the files in ftp to my local folder.All the files should be deleted in ftp once downloaded to local drive.
从下面的code
-
我可以从FTP仅有一个文件,其中我没有料到
I can download only a file from ftp where I am not expecting
我需要放置在本地文件名的名称在一个文件夹,但不是所有的文件。
I need to place all the files in a folder but not in the name of local file name.
我的code:
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential("ftpusername", "ftp pass");
ftpClient.DownloadFile("ftp://ftpdetails.com/dec.docx",@"D:\\Mainfolder\test.docx");
}
从上面的code,我可以下载一个文件,并将其放置在文件名only..Where我有这么多的文件,从FTP下载,并将其放置在本地folder..Any建议非常多谢天谢地了。
From the above code, i can download a file and place it in the name of file only..Where I have so many files to download from ftp and place it in a local folder..Any suggestions very much thankful.
下面是一个使用FTPWebResponse获取文件名列表从目录中的一个例子:
Here's an example of using the FTPWebResponse to get a list of file names from a directory:
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.funet.fi/pub/standards/RFC/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{
String filename = reader.ReadLine();
Console.WriteLine(filename);
//you now have the file name, you can use it to download this specific file
}
Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
}
}
您可以使用这个列表下载每个文件。请注意,如果你有很多要下载的文件,你可能想看看 asyncronous下载以加快速度 - 但你试图实现任何异步的东西之前,我会得到这个工作的第一个
You can then use this list to download each file. Note that if you have a lot of files to download, you may want to look into asyncronous downloading to speed things up - but I would get this working first before you attempt to implement any async stuff.