连接到FTP并在.txt中搜索文件

时间:2013-11-17 17:24:56

标签: c# ftp ftp-server

我试图创建一个程序,让您检查该ftp服务器上特定文件扩展的可用性,然后对那些已打开文件的文件进行排序。

这就是我到目前为止尝试这样做的方式:

string path;
path = "ftp://" + textBox1.Text + "/";
string[] files = System.IO.Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);

1 个答案:

答案 0 :(得分:3)

FtpWebRequest ftpRequest = 
    (FtpWebRequest)WebRequest.Create("ftp://ftp.freebsd.org/pub/FreeBSD/");

ftpRequest.Credentials = new NetworkCredential("anonymous", "k3rnel31@k.com");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

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

string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
    if (line.Contains(".txt")) 
    {
        MessageBox.Show(line); 
        line = streamReader.ReadLine();
        filestxt.Add(line);
    } 
    else 
    { 
        line = streamReader.ReadLine(); 
    }
}

streamReader.Close();