我想通过FTP使用c#从文件夹中获取文件,我的文件夹名称为 MyFolder ,在此文件夹中我有多个文件夹,我需要从所有这些文件夹中获取每个文件在我的 MyFolder .Below代码中,我获取所有目录,现在我需要获取每个文件。
Eg:httpdocs/Myfolder/newfolder/newfile.txt
/newfile1.txt
/newfile2.txt
httpdocs/Myfolder/newfolder1/newfile.txt
httpdocs/Myfolder/newfolder2/newfile.txt
FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create("ftp://www.xxxxxxx.com/httpdocs/MyFolder");
ftpRequest.Credentials = new NetworkCredential("xxxxx", "xxxxxx");
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();
}
答案 0 :(得分:-1)
您是否看过MSDN文档? https://msdn.microsoft.com/de-de/library/ms229711(v=vs.110).aspx
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// 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);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
修改:Already on StackOverflow看看这里。