[英]Downloading multiple files using FTP in C#
I need to write a routines that downloads a given directory on the server completely - all the files and directories in it. 我需要编写一个例程,以完全下载服务器上的给定目录-其中的所有文件和目录。
Right now I have a routine that lists dir content 现在我有一个列出目录内容的例程
public List<string> GetListOfFiles(string serverPath)
{
List<string> files = new List<string>();
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + serverPath);
request.Credentials = new NetworkCredential(_domain + "\\" + _username, _password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
files.Add(line);
line = reader.ReadLine();
}
response.Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
string exMsg = string.Empty;
switch (response.StatusCode)
{
case FtpStatusCode.NotLoggedIn:
exMsg = "wrong username/password";
break;
default:
exMsg = "The server is inaccessible or taking too long to respond.";
break;
}
throw new Exception(exMsg);
}
return files;
}
The issue is that I get the list of files and directories...so something like 问题是我得到了文件和目录的列表...类似
file1.dll 文件1.dll
file2.dll file2.dll
dir1Name dir1Name
Is there a way to make a distinction between a file name and a directory name when listing it? 列出文件时,有没有办法区分文件名和目录名? Like a flag?
像一个标志?
Unfortunately, the returned information is really a function of your FTP server, not the framework. 不幸的是,返回的信息实际上是FTP服务器的功能,而不是框架的功能。
You can ListDirectoryDetails
instead of ListDirectory
, which should provide you much more detailed information (including whether each file is a directory or a file), but would require special parsing, as its format, too, is dependent on the FTP Server. 您可以使用
ListDirectoryDetails
而不是ListDirectory
,它应该为您提供更多详细信息(包括每个文件是目录还是文件),但是由于其格式也取决于FTP服务器,因此需要特殊的分析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.