Solved

FTP Client

Posted on 2006-06-09
8
403 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

790 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