繁体   English   中英

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

[英]How to search of specified Extension files on ftp server?

如何使函数为此支持参数

 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
}

有什么办法吗?

这是返回列表的函数的解决方案

 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;

发生错误,您需要执行

results.Add(line);

在您致电之前

results.Add(line);

获取正确的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM