Solved

FTP Client

Posted on 2006-06-09
8
401 Views
Last Modified: 2010-04-16
How does one list the directory on an ftp server and then download the contents, is it similiar to uploading? this is the code i have for my upload method:

           DirectoryInfo hostDir = new DirectoryInfo(hostDirectory);
            FileInfo[] hostInfos = hostDir.GetFiles("*.txt");
            bool IsHostSuccess = true;

            foreach (FileInfo hostFileInfo in hostInfos)
            {
                if (null != hostFileInfo)
                {
                    IsHostSuccess = UploadFile(hostFileInfo);
                    if (!IsHostSuccess)
                    {
                        errCount = errCount + 1;
                        if (errCount > 100)
                            break;
                    }
                }
            }

I basically want to do the same exact thing except downloading and instead of listing the current dictory locally, listing the directory remotely.
0
Comment
Question by:JoeDW
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 16870540
You can find several FTP implementations on the web, which can do all these actions for you.

For example:
http://www.codeproject.com/cs/internet/ftplib.asp
http://www.codeproject.com/cs/internet/ftplibrary.asp
0
 
LVL 1

Author Comment

by:JoeDW
ID: 16870590
I'd really rather not use someone elses, as I am trying to learn this on my own and would just like to see some examples...I mean the application is complete asside from listing the directory. This isnt a full FTP client, it's simply a small app that uses the FTP protocol to upload like 6 files and short of having to hard code those file names in there I would like to just list the directory for *.txt files, if possible without using an entire library.

Thanks
Joe
0
 
LVL 14

Expert Comment

by:existenz2
ID: 16871985
In that case I suggest you to read the FTP RFC: http://www.faqs.org/rfcs/rfc959.html and the given urls as examples.
0
 
LVL 1

Author Comment

by:JoeDW
ID: 16872082
Well if I wasn't strapped for time and had a few days to read over it that would be fine, but then I wouldn't have posted my question to Experts Exchange would I? I am not trying to be rude but the entire reason for posting here is usually to find answers, fast. I am sure that is a very good read but for what I am trying to accomplish its overkill. I have since figured it out and simply used the Ftp ListDirectory and Regular Expressions to match for the files I am looking for

public string ListDirectory()
        {
            string files = null;
            NetworkCredential credentials = new NetworkCredential(username, password);
            string serverAddress = "ftp://" + server + "/";
            FtpWebRequest request = null;
            FtpWebResponse response = null;

            request = (FtpWebRequest)WebRequest.Create(new Uri(serverAddress));

            request.Method = WebRequestMethods.Ftp.ListDirectory;

            request.Credentials = credentials;
            response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            string [] fileArray = new string[] { reader.ReadToEnd() };

            foreach (string file in fileArray)
            {
                files = file;
            }

            reader.Close();
            response.Close();
         
            return files;
         
  }

Thanks anyways
Joe
0
 
LVL 14

Accepted Solution

by:
existenz2 earned 250 total points
ID: 16872189
Uploading files is more complex then downloading. You need to initiate quite some stuff. The given samples show you exactly how it can be done. Another example: http://www.csharphelp.com/archives/archive219.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A short film showing how OnPage and Connectwise integration works.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now