Solved

FTP Client

Posted on 2006-06-09
8
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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