user3002135
user3002135

Reputation: 237

Connect to FTP and search for files in .txt

Im trying to make a program that lets you check the availibility of a specific file extention on that ftp server and then sort the ones out that have the files on.

this is how i tried to do it so far:

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

Upvotes: 2

Views: 3520

Answers (1)

Hichem
Hichem

Reputation: 1182

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

ftpRequest.Credentials = new NetworkCredential("anonymous", "[email protected]");
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();

Upvotes: 3

Related Questions