如何在ftp服务器上搜索指定的扩展文件?

时间:2013-11-18 19:09:04

标签: c# search ftp

如何使函数支持此

的参数
 public List<string> getfiles(string FTPhostname, string FTPpath,string FTPusername,string FTPpassword,string extension)
{
 'request to ftp hostname 
 'get response from 
 'list all directories and files
 'search for spesified extension files
}

任何方式吗?

2 个答案:

答案 0 :(得分:1)

这里是一个返回List

的函数的解决方案
 public List<string> getfiles(string FTPhostname, string FTPpath,string FTPusername,string FTPpassword,string extension)
    {
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + FTPhostname + "/" + FTPpath );
        ftpRequest.Credentials = new NetworkCredential(FTPusername , FTPpassword );
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; 

        FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());

        List<string> results = new List<string>();

        string line = streamReader.ReadLine();
        while (!string.IsNullOrEmpty(line))
        {

            if (line.Contains(extension)) {
                line = streamReader.ReadLine();
                results.Add(line);}

            else {line = streamReader.ReadLine(); }
                           }

        streamReader.Close();
        return results;
    }

然后为ex:png文件

调用此函数
  String[] txtfounds= getfiles("ftp.piacton.com", "/Public/Software/Official/LightField/Archives/", "anonymous", "k3tnel31@k.com", ".png").ToArray();

如果要将此结果添加到lisbox:

listBox1.DataSource = txtfounds;

答案 1 :(得分:0)

有错误,您需要执行

results.Add(line);

之前打电话给

results.Add(line);

获取正确的文件。